Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validate two fields with jQuery Validate

I'm using jQuery Validate Plugin: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

Simple demo:

  <input type="text" name="email" class="required email" />
  <input type="text" name="email_confirm" class="required email" />

How to check if the value in the two inputs are the same with jQuery or with jQuery Validate Plugin.

I just tried equalto and it works:

rules: {
  email: 'required',
  email2: {equalTo: '.email'}
like image 657
Cheerio Avatar asked May 17 '11 08:05

Cheerio


1 Answers

By looking at the demo page at http://jquery.bassistance.de/validate/demo/ you can validate 2 fields that should be equal by using "equalTo":

    $("#signupForm").validate({
    rules: {
                    ...snip... 
        password: {
            required: true,
            minlength: 5
        },
        confirm_password: {
            required: true,
            minlength: 5,
            equalTo: "#password"
        },
                   ...snip...
    },
    messages: {
        ...snip...
        password: {
            required: "Please provide a password",
            minlength: "Your password must be at least 5 characters long"
        },
        confirm_password: {
            required: "Please provide a password",
            minlength: "Your password must be at least 5 characters long",
            equalTo: "Please enter the same password as above"
        },
        ...snip...
    }
});

To do it using the class method, you just need to find the syntax. I couldn't find it for the equalTo attribute, but it is probably something like:

class="required email equalTo['email']"
like image 100
psx Avatar answered Sep 19 '22 22:09

psx