From the course: Validating and Processing Forms with JavaScript and PHP
Unlock this course with a free trial
Join today to access over 22,400 courses taught by industry experts.
Creating a generic input validation function
From the course: Validating and Processing Forms with JavaScript and PHP
Creating a generic input validation function
In the last movie, we added dynamic validation to our form with regular expressions, so we can give the users some feedback while they're typing out in the form. So if I come over here and I try to type in something other than a telephone number. And when I hit the tab key, it says input does not match expected pattern. This works fine, but we could make it be a lot more efficient, and also easily make it work with any of our other input fields. So, let's take a look at how we can take this concept. And generalize it to work with a range of fields. We're using a pattern for detecting phone numbers from the website html5forms.com. Matter of fact, we use the same pattern twice, once in our index.html file, you can see it if you scroll down to the telephone field that there's a pattern right here. And we're also using it in our script. And that is right here. I have to escape some of those characters in order to make it work. So we can simplify this by having JavaScript read the pattern…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
Accessing forms3m 57s
-
Looking up form elements3m 35s
-
Handling focus changes2m 47s
-
Detecting the onchange event4m 31s
-
Using the selectedIndex property2m 30s
-
Dynamic validation with regular expressions7m
-
Creating a generic input validation function4m 31s
-
Validating in older browsers with Modernizr7m 32s
-
Interrupting form submission with onsubmit5m 50s
-
-
-
-
-