New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Validating on submit with jQuery

From: Validating and Processing Forms with JavaScript and PHP

Video: Validating on submit with jQuery

Let's get busy implementing some validation using jQuery. I'm going to start off with the same form from chapter three but I've added decode to include jQuery right here. My javascript file right now is empty so we'll start with that. First things first, whenever we are working with JQuery, we need to do things after both jQuery and the entire page have loaded. JQuery has a special event called ready. We'll start our form script by using that method. We want to capture the Submit event so we are going to use JQuery's Submit method.

Validating on submit with jQuery

Let's get busy implementing some validation using jQuery. I'm going to start off with the same form from chapter three but I've added decode to include jQuery right here. My javascript file right now is empty so we'll start with that. First things first, whenever we are working with JQuery, we need to do things after both jQuery and the entire page have loaded. JQuery has a special event called ready. We'll start our form script by using that method. We want to capture the Submit event so we are going to use JQuery's Submit method.

To make things more backwards compatible, it's always better to use jQuerys methods instead of java scripts. They're built to be more compatible with older browsers. We can easily make sure that we choose each required field by using this selector. So we're going to choose all the input fields that have the required attribute in them. Now, I want to go through each of the fields with the required attribute so we can use jQuery's Each method. When we're using the Each method, jQuery will try to go through each element of the inputs that it picked sequentially. We can deal with the element as the loop is executing by using the This selector. To get the value of the field, we're going to use the Val method. So to check to see if this field is going to be empty, and we can check by typing in this and then use the value function.

So we're going to check to see if the value of the current field is empty. If it is, then we're going to do something right here. In the java script version of this script, what we did before was output things to an output variable that was at the top of the window right here. With jig query it's super easy to place things before or after an existing element. There are two methods named one Before and the other one After that let you do that. So, we're going to add a div after the current input field to give the user feedback right next to the input field as opposed to at the top of the form.

We'll give it a class of error just like before and type in a message. There's one more thing we need do. We haven't really prevented the form from submitting so I'm going to add an abort variable. We'll set it to false at the top of the Submit method. And if any of the input fields are not filled out, we'll set that variable to true. After we go through each field, we'll do an if statement that prevents the form from submitting, if any of the required fields haven't been filled out.

Now if somebody doesn't fill out one of the fields a second time and tries to submit the form they are going to keep on getting errors. So we have to delete all the errors and the page. This is one of those things that are super easy with jQuery. You can ask for it to remove all the dibs with a class of error in this way. So we'll do this at the very top of the page before we check each input, so that jQuery can clear any errors that are currently on the page. So I'm going to save this. So if I go back into this form, I'm using the chrome browser because the chrome browser understands HTML 5 validation.

You're not going to see a change in this form. So if I try and submit this form by itself, notice that the HTML5 validation, is still taking over. I'm going to show you this form in an old version of Internet Explorer, so you can see how my validation works, in that browser. Here I am on an old version of Internet Explorer. This happens to be IE9 and if I try to scroll down and submit the form without filling the required fields. it's giving me the errors, and it's doing so right underneath each required input field.

So that's definitely an improvement over what we were able to do with plain JavaScript.

Show transcript

This video is part of

Image for Validating and Processing Forms with JavaScript and PHP
 
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

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

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
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.