开发者

Custom jQuery Validation Method

开发者 https://www.devze.com 2023-02-28 16:06 出处:网络
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 inp开发者_高级运维uts

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 inp开发者_高级运维uts 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');


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消