Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the previous movie, we created validation when the user submits the form. Now I'm going to show you how to handle more interactive validation. This time we're going to check for validation when the user exits a field and has a placeholder attribute. So I'm going to come right here and I'm going to do the dollar sign again. And I'm going to type in input. And then use the type selector which are these brackets and type in place holder that way we'll look for fields that are input fields and also have a place holder. And then we'll attach the blur method. The blur method will happen when somebody exits a field. So this will execute a function.
We'll see if it's a positive number. If it's a negative number, then the pattern hasn't been matched, so we'll check for that in an if statement. So if the pattern is not valid, then I'm going to execute some code right here. And that code will be to insert some HTML displaying the error right after the current field, so sort of like what we did up here. So I'll copy that code and change this text. Now, if it's not valid, we can also do one more thing. I'm going to move the insertion point to the field that doesn't match the current pattern.
Let's also add some comments. Now I'm going to Save this and I'll go back into my form. I'll refresh the page and I'll try to type something that doesn't match the pattern. Notice that its telling me that the entry does not match the expected pattern. And it reads the pattern value. And it won't let me go to another field until the pattern's correct. Same thing with the telephone number. Once again, our script is a lot shorter when we use the jquery library. As a byproduct, it's also more compatible. For example, older browsers support HTML attributes like pattern and placeholder inconsistently.
Although it has a lot of other benefits, adding a library to your projects is going to increase the file size of your page's download. So, when using Jquery, you'll have to weigh whether the added features warrant the slightly larger download size for your users.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96849 Viewers
56 Video lessons · 110541 Viewers
71 Video lessons · 79380 Viewers
131 Video lessons · 38078 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.