Your code is working. You should also assign a rule to your field when you initialize the plugin with .validate().
Working DEMO: http://jsfiddle.net/KrLkF/
$(document).ready(function () {
$.validator.addMethod("passwordCheck", function (value, element) {
return value == $("#pnameTxt").val();
}, 'Password and Confirm Password should be same');
$('#myform').validate({
rules: {
pnameTxt2: {
passwordCheck: true
}
}
});
});
HOWEVER , you do not need to write your own method for this function. The jQuery Validate plugin already has a rule equalToand here is how to use it.
Working DEMO: http://jsfiddle.net/tdhHt/
$(document).ready(function () {
$('#myform').validate({
rules: {
pnameTxt2: {
equalTo: "#pnameTxt"
}
},
messages: {
pnameTxt2: {
equalTo: "Password and Confirm Password should be same"
}
}
});
});
source
share