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

Validating and Processing Forms with JavaScript and PHP

Using the jQuery Validation plugin


From:

Validating and Processing Forms with JavaScript and PHP

with Ray Villalobos

Video: Using the jQuery Validation plugin

One of the common ways to add functionality to jQuery is by using plugins. They are additional Javascript files which expand the library. One of the oldest plugins available for jQuery happens to be a form validation plugin. If you're already using jQuery on your page, this might be a really quick solution for you since it's fairly easy to set up. You could download the plugin from the jQuery validation page. To install it just like with any other script, you create a script tag with the URL of the library as the source. We're going to go ahead and add it from a content delivery network, so that we don't have to download the script.
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 the jQuery Validation plugin

One of the common ways to add functionality to jQuery is by using plugins. They are additional Javascript files which expand the library. One of the oldest plugins available for jQuery happens to be a form validation plugin. If you're already using jQuery on your page, this might be a really quick solution for you since it's fairly easy to set up. You could download the plugin from the jQuery validation page. To install it just like with any other script, you create a script tag with the URL of the library as the source. We're going to go ahead and add it from a content delivery network, so that we don't have to download the script.

I'm going to go to cdnjs.com and I'll do a search for validate. You can see the jQuery validate library right here. So I'm going to copy this URL, and go back into my form and I'll paste it right after the jQuery code. Now I'm going to add the HTTP colon right here. Notice some problems if I don't add the HTTP colon to this link. Now adding validation to our form is going to be ridiculously simple. All we have to do is identify the form within the jQuery global variable. And send it to the validate method at the validate plugin created for us.

So I'm going to go all the way to the bottom and I'm going to replace my current script tag, which is calling my validation script. And first, I'll call the document ready function, and after that, I'll use the jQuery dollar sign global method. And I'll pass it the location of my form, which is a form with an ID of my form. And that's pretty much it by adding the validate method and targeting my form, you've added all kinds of functionality to our existing page. So I'm going to save this and go over to my form and I'll refresh and now when I try to type in say, an email, if I don't type a correct email, it's going to tell me please enter a valid email address.

Notice that it's even making this font automatically red for us. So if I type a correct email address, the information goes away. Now if I try to submit the form, it'll tell me to please enter your name. So it noticed that this field was a type text field and it had a name alike my name and it automatically detected it as a required field. Notice that I can type this with a way that doesn't follow the requested pattern. And I can submit the form. It's going to go ahead and submit the form. And it's not given me the live feedback that I had when I created my own script.

So you don't have all the functionality that you have with your own script. You didn't really have to write any JavaScript or jQuery at all. This is already given you a lot of benefits. This plugin does depend on you coding the form properly. With labels and ID's for form elements. You can control and enhance your validation by adding a Javascript object. Let's say we want to add another password field to verify the user typed in a password and that it was at least eight characters in length, and that it also matched the original password field, so I'm going to go into the code for this, over to the password.

And I'm going to copy this list item right here, and just paste it one more time. I'll change the name of this right here, to my password conf. And I'll change the ID as well, so that these are related to each other, and the name of it will also be my password conf. So, I'm going to save this, and I'm going to go all the way to the bottom and modify the validate method that's being called right here, to add some functionality. So, what we're going to do is, pass it a rules JavaScript object, and that rules is going to be another object. And we're going to pass it some rules.

So, what I want to do is tell my password field has to be at least six characters in length, and that it's going to be a required field. Now, I'm going to copy this field and paste it right here. And I'll modify it to save my password, conf. So my password confirmation, I'm going to delete the minimum length, and just tell it that this is going to be, equal to, the field called, my password.

Take out this comma. So my password field is going to have a minimum length of six. It's going to be a required field, and the password confirmation is going to have to match whatever the password was. So lemme go ahead and save this and go back into my form. I'll refresh, and I'll try to type in, just the password here. Then I'll try to type in some other password that doesn't match. If I do that then it's asking me to make sure I set the same value.

Let me try to type a short password. Now it says that I need to enter at least six characters and also match the password again. With a simple Javascript object I am able to add additional functionality to my form without writing a lot of code. Do remember that whenever you add a plugin, you're adding both size and complexity to your pages. So you need to balance the ease of implementation with the growing size of your pages. We're not just adding the jQuery library, we're adding the jQuery library, plus an additional validation plugin. This is however, a great way to get some decent browser validation without a lot of work.

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