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.
Constraining with regular expression patterns
From the course: Validating and Processing Forms with JavaScript and PHP
Constraining with regular expression patterns
HTML5 input fields have a super cool attribute called Pattern, which you can use inside fields to check the user's input against a specific set of characters. Inside this attribute, you define a string that prevents the form from submitting unless the text in the input field matches the pattern. The language we use to format our string is called a regular expression. And they're common to a variety of programming languages. Let's take a look at how this works. I have a very simple form here with a single field that expects you to type in a correct pattern. So let's go to the code for this. And I'm going to add the Pattern attribute to this field. Now I'm going to add some literal text here. Texas and save this. Go back into my form, make sure I refresh, and I'm going to try to type something into the form. I'll try California. And when I hit Enter, I get a popup that says to please match the requested format. So I'm going to try typing Texas now and hit Enter. You see that I still get…
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.