Start learning with our library of video tutorials taught by experts. Get started

Validating and Processing Forms with JavaScript and PHP

Constraining with regular expression patterns


From:

Validating and Processing Forms with JavaScript and PHP

with Ray Villalobos

Video: 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.
Expand all | Collapse all
  1. 3m 36s
    1. Welcome
      1m 9s
    2. What you should know
      1m 2s
    3. Using the exercise files
      1m 25s
  2. 12m 50s
    1. Understanding forms
      2m 2s
    2. Working with form fields
      7m 4s
    3. Using the form tag
      3m 44s
  3. 19m 23s
    1. Using input validation
      2m 10s
    2. Adding required fields and placeholders
      1m 31s
    3. Constraining numeric and date fields
      2m 32s
    4. Accepting multiple entries
      1m 41s
    5. Limiting uploads by MIME types
      2m 35s
    6. Assisting text input with a datalist
      1m 55s
    7. Constraining with regular expression patterns
      6m 59s
  4. 42m 13s
    1. Accessing forms
      3m 57s
    2. Looking up form elements
      3m 35s
    3. Handling focus changes
      2m 47s
    4. Detecting the onchange event
      4m 31s
    5. Using the selectedIndex property
      2m 30s
    6. Dynamic validation with regular expressions
      7m 0s
    7. Creating a generic input validation function
      4m 31s
    8. Validating in older browsers with Modernizr
      7m 32s
    9. Interrupting form submission with onsubmit
      5m 50s
  5. 15m 20s
    1. Understanding jQuery
      3m 47s
    2. Validating on submit with jQuery
      3m 45s
    3. Building interactive jQuery validation
      2m 34s
    4. Using the jQuery Validation plugin
      5m 14s
  6. 32m 57s
    1. Communicating with PHP servers
      2m 27s
    2. Retrieving data from superglobals
      8m 18s
    3. Using server-side validation
      4m 59s
    4. Adding in-page validation
      5m 22s
    5. Mirroring input data back to the user
      7m 46s
    6. Sanitizing form input
      4m 5s
  7. 43m 29s
    1. Mailing form data
      8m 28s
    2. Understanding file uploads
      3m 1s
    3. Uploading files
      9m 20s
    4. Processing form data with AJAX
      8m 14s
    5. Preparing your database
      5m 50s
    6. Pushing data
      8m 36s
  8. 1m 17s
    1. Next steps
      1m 17s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
Validating and Processing Forms with JavaScript and PHP
2h 51m Intermediate Apr 23, 2013

Viewers: in countries Watching now:

Validating web forms is a critical skill for any web developer, ensuring that the data that's submitted is complete, accurate, and nonmalicious before it's sent off to the server. Join author Ray Villalobos in this course as he shows how to validate input from site visitors with HTML5, JavaScript, and jQuery and then process the data with PHP. Plus, learn how to email form data and save it in a MySQL database so that it's ready for other applications.

Topics include:
  • Understanding forms
  • Adding required fields and placeholders
  • Accepting multiple entries
  • Limiting uploads
  • Handling focus changes
  • Validating with regular expressions
  • Working with older browsers
  • Building jQuery validation
  • Using server-side validation
  • Sanitizing form input
  • Uploading files
  • Sending form data to a database
Subjects:
Developer Web Web Design Databases Web Development
Software:
JavaScript PHP
Author:
Ray Villalobos

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 please match the requested format because when I typed it into pattern, I typed in a capital T. So when I type a capital T it works just fine.

Now that's not particularly useful but it gives you the idea that the patterns expect the text to match. To create regular expression patterns you use regular characters combined with special characters. For example the period means match any character. So if we replace the capital T with a period, I'm going to save, come back here, refresh, and I'll try typing in lower case Texas this time, and that works just fine. If I type in upper case Texas, that works, too. But now I can type something in like Lexus, and that'll be acceptable as well, and that's not necessarily what we want.

You can ask for a specific series of characters together by placing them within brackets. So, if we wanted to match Texas, upper or lowercase, you can type in something like, brackets, and then capital T, lowercase T, and that will accept either lowercase Texas or uppercase Texas. So, I'll save, come back here, and now I can type in Texas, capital Texas, but not Lexus, and that's exactly what we want. You can also create a range. So let's say you're looking for a single lower case alphabetical character.

You do that using a range. So, let's try typing in lower case A, then the dash, and then Z right here. I'll save this and go back into my form. Now I can type in any character, I'll try K, that works pretty well. I can try typing the A. Now, let's try an upper case character, like T, and it doesn't like that. It also doesn't let me do more than one character, just a single character. It's not necessarily good. If you wanted to match one or more characters you could do something like this, just put a plus sign at the end of the pattern and then it will take one or more of this particular sequence. So save that and I'll refresh, and now I can type in multiple lower case characters but not any upper case characters in that sequence. So, what if you wanted upper case letters too.

Well, we could put in more than one sequence of characters as well. So, you could say something like, capital A through Z and then lower case A through Z. So let's try that. Come over here, and refresh, and try, and this works well, for any combination of upper and lowercase characters. Now, what if we wanted numbers? Well, that would work by adding, of course, a number range, like zero through nine. So save it, come back here, refresh. And now we could do any number of upper case, lower case characters or numbers. Now it's not going to accept spaces in our pattern. So, we could do that just by adding a space to the pattern. So just enter space right there.

Save it. Refresh. And that works just fine. That's pretty cool, but what if you wanted to ask for a specific number of characters? Perhaps something like a zip code. You could ask for a certain amount of characters with curly brackets. So let's try that. We'll do zero through nine, and instead of the plus sign we'll use curly brackets and a number. So we'll say we want five digits from zero to nine. So I'll save that. I'll try typing in here, refresh one, two, three.

That doesn't work, but I do one, two, three, four, five and it takes it just fine. So, that would be perfect for matching a zip code. There's a shortcut for numbers, so that could be written instead of like this, like this. That means a digit five times. So, if you wanted to do something like a zip plus four code pattern it would look something like this. Notice then when I'm using the dash I need to escape it with the slash right before it. So I need a series of five numbers followed by a dash, followed by another number, four times. So let me save that, refresh and.

We trying typing in some numbers here. That's not going to work. So it should be something like five numbers, dash, four numbers, and then it will work. This is obviously getting super exciting and complicated at the same time. Now I could spend a whole course explaining all the rules of regular expressions, thankfully somebody's already done that. You can check out our course on using regular expressions at this page. There's also a whole bunch of really cool resources you can find online related to regular expressions.

For example, here's a really good cheat sheet with a quick explanation of the patterns. If you want to build out and test your regular expressions, you can use this online tool called, Regexpal. Finally, this website has a number of pre-built HTML 5 patterns you can use for common tasks. So let's take a look at postal codes. And you can see that I got pretty close to it, however, with postal codes you can ask for either a five digit number or alternatively, a five digit number with a dash, and the four digit number.

So, if we want to use this pattern for American Postal Codes, you just double-click, copy it, and bring it back here, and paste this right here. Make sure you get rid of that carriage return, and save it. Come back here. Refresh and we'll try some different things. So I'll just try three numbers, that doesn't work. Five numbers works and, five numbers plus the dash, and four numbers works as well.

There's a lot of other really cool regular expressions on this website, so you can look for things like regular expressions for names, passwords, dates, phones, etc. As I mentioned before, regular expressions are common to a lot of language, including HTML5 and Javascript. So we'll talk a little bit more about them in the chapter on validating with Javascript.

There are currently no FAQs about Validating and Processing Forms with JavaScript and PHP.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Validating and Processing Forms with JavaScript and PHP.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked