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.

Dynamic validation with regular expressions

Dynamic validation with regular expressions

Regular expressions give you a way to search for patterns in form fields. And that can help with validation and processing. I talked about regular expressions in the movie on constraining with regular expression patterns. The regular expression syntax in HTML5 is pretty similar to JavaScript. As a matter of fact, the HTML5 implementation is a copy of the JavaScript implementation, so you can use them mostly interchangeably. In JavaScript, you have to create a regular expression pattern and then use the search method to search for that pattern in an input field. So, you might be asking yourself, why do I need to validate for patterns, if I can just use the pattern attribute in HTML5. Well, with validation, it's important to try to be as compatible with older browsers as possible. Using regular expressions in JavaScript, will let you build validation that's compatible with older browsers. Another reason is that the pattern attribute, prevents the form from submitting but it doesn't…

Contents