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

Validating and Processing Forms with JavaScript and PHP

Using server-side validation


From:

Validating and Processing Forms with JavaScript and PHP

with Ray Villalobos

Video: Using server-side validation

Now that we know how to access variables, let's take a look at how we would validate data in different ways. I'm going to show you how to check for a required field, a minimum number of characters, make sure that our passwords match, and use regular expressions for validation. So, I'm going to set up some variables in my process.php file. I'm using the files from the previous movie, so I'm going to go ahead and delete everything right here. And start by creating some variables. The input fields that we're going to work with are: the name field, the password field, and the password confirmation field.
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

Using server-side validation

Now that we know how to access variables, let's take a look at how we would validate data in different ways. I'm going to show you how to check for a required field, a minimum number of characters, make sure that our passwords match, and use regular expressions for validation. So, I'm going to set up some variables in my process.php file. I'm using the files from the previous movie, so I'm going to go ahead and delete everything right here. And start by creating some variables. The input fields that we're going to work with are: the name field, the password field, and the password confirmation field.

So I'll create variables for those. I'll check the request, that we got from the server, and look for the variable, my name, from the form. I can do the same thing for the password and the password confirmation. The easiest type of validation to do is check for required field. All you have to do is make sure that the string is not empty. So, let's do that with the name. So if the name is empty, I'm going to echo something, or output something to the screen. And it's going to be a div, that says your name is a required field.

So notice then, I'm also using this if and endif notation. I just like it a little bit better than these curly brackets. Sometimes these can get lost when the coding gets a little bit complicated. I'm going to save this, and then come over here to the form and refresh. And I'm going to try to send the form without filling anything out. So I should get an error that says, sorry, your name is a required field. Guess I'll hit the Back button, and let me do another type of validation. We can check to see if the field has a certain number of characters, with PHP's string length method. To learn more about this method, make sure you check out the PHP documentation. So, with that method, we'll create another if statement. And we'll check to see that the string length, of the password string, is at least six characters.

If it's less than six characters, I'll output an error message. So let me save that, and come back here. I'll refresh, and I'll go ahead and type something for the name. I'll leave the password blank. Now it's complaning that the password has to be at least six characters. If I try to not type a name or the password, I should see both errors. So let's try to type in a password that is at least six characters. And that's working fine, because it's complaining that I didn't put in a name.

Perfect. To make sure our passwords match, we can simply compare the two passwords with each other. If they don't match, we can't output an error. All right, so, let's go ahead and save this, and refresh the page. And I'll try to send the form without any fields. It's not going to complain about the passwords, because they are matching, even though they're empty, they're technically matching. So we need to make them not match. I'll go ahead and type in something that definitely doesn't match, but it's at least six characters.

So now it says, sorry, passwords must match. Finally, we can also do a regular expression match comparison. We've already talked about regular expressions in the chapters on constraining with regular expression patterns and dynamic validation with regular expressions. They allow you to do complex pattern comparisons. As a matter of fact, we've got a whole course on regular expressions. To match the text from our forms, we're going to use PHP's preg-match method, which you can find right here. Now, it looks really complicated, but it's fairly simple to implement. Let's go back into our form, and we'll create another area right here. So what I'll do is, I'll create an if statement.

I'm going to check to see if a certain pattern does not validate. The preg-match method is going to compare a pattern with a variable, and if they match it's going to return one. If it doesn't match, it's going to return zero. So you want to check to make sure that it doesn't match. So first, you pass it a pattern, and then we pass it the variable that we want to compare. In this case, we're going to do the name variable. So we know that that one is called myname. Now, the pattern we want to pass is a little different than what we've been doing with HTML and JavaScript. You have to include the pattern in slashes.

Other then that, it's the same pattern that we used on some of the other movies. Just any number of letters, followed by a comma and a space, and then any number of letters. Now, if the pattern does not match, then we'll output an error. So let me go ahead and save this. And I'm going to come back here, hit the Back button, refresh my page, and try to type in something that is not in this format. So, we'll just type in a name like this, hit Send. And it says that it must be in the format last comma first, which is what we want.

So validation's pretty easy with PHP. With the addition of regular expression patterns, you can really get quick picky about how you compare the user's input with certain patterns.

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