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

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…

Contents