Ryan
Ryan

Reputation: 1338

Custom jQuery Validation Method

I've got two input fields, email and email_confirm. The email field has the value of the users email from the database. I want to make sure if the user updates their email (both inputs have to have the same value) that they match, however, I can't use the defaul equalTo because email input always has a value. So I need to check if email_confirm is equalTo email IF the email value is different to the default value.

Here's the code I have, value seems to be empty always also.

$.validator.addMethod('myEqual', function (value, element, param){
        return value == $(param).val();
}, 'Message');

Upvotes: 1

Views: 308

Answers (2)

user405398
user405398

Reputation:

Just add the below rule for email_confirm field,

equalTo : "#email" //Replace #email with the id the of the email field.

No need to add custom method for that.

Upvotes: 2

Nick Brunt
Nick Brunt

Reputation: 10077

if ($("#email").val() != theDefaultValue) {
  if ($("#email").val() == $("#email_confirm")) {
    // It's all good
  } else {
    // It's all bad
  }
} else {
  // It doesn't matter
}

Upvotes: 0

Related Questions