33

I have an e-mail field, and a confirm e-mail field. I need to validate both of them to make sure their values match.

Is there a way to add a rule to match those two fields?

Steven
  • 17,851
  • 66
  • 182
  • 285
  • 1
    http://stackoverflow.com/questions/931687/using-jquery-validate-plugin-to-validate-multiple-form-fields-with-identical-name – Zirak Feb 28 '11 at 20:57

3 Answers3

71

You could use the equalTo method:

$('#myform').validate({
    rules: {
        email: 'required',
        emailConfirm: {
            equalTo: '#email'
        }
    }
});
Darin Dimitrov
  • 994,864
  • 265
  • 3,241
  • 2,902
  • can you tell how to add custom message for this? – sreemanth pulagam Dec 31 '13 at 13:36
  • 1
    Sure, use the `messages` attribute. I strongly suggest you to reading the documentation of the jquery validate plugin which contains all the necessary details: http://jqueryvalidation.org/documentation/ – Darin Dimitrov Dec 31 '13 at 13:37
  • @sreemanthpulagam Yep, see http://stackoverflow.com/questions/6777634/jquery-validation-plugin-custom-message – crmpicco Feb 11 '14 at 16:51
24

You can also do this on the second field:

 class="required email" equalTo='#email'
Jazzy
  • 5,804
  • 10
  • 48
  • 72
2

U can use this

email: {
      required: true, email: true
      },
c_email: { 
                required: true, equalTo: "#email", minlength: 5
          },

<div class="form-row"><span class="label">email</span><input type="email" name="email" class="required" id="email" /></div>

<div class="form-row"><span class="label">Confirm email</span><input type="email" name="c_email" id="c_email" /></div>
Naresh Kumar
  • 355
  • 3
  • 6