I have searched stack overflow and can't find an answer to my question.
I am using bassistance jquery validation plugin, and it is working well all except for one particular section, custom methods.
The methods are working it is just that calling $('selector').valid() is telling me the element is invalid, even though it should be valid.
This is due to the this.optional(element) returning "dependency-mismatch".
My question is:
What does dependency-mismatch mean when checking for optional on an element?
Does dependency mean that it is optional, but has other dependencies?
So should my custom method check the optional field for "dependency-mismatch"??
I am unclear what it is suppose to return and why.
should my custom method also return dependency-mismatch? or should I deal with it??
This is my custom method:
$.validator.addMethod(
"ukphonenumber",
function (value, element, validate) {
if (validate) {
var regexp = "\\s*\\(?((\\+0?44)?\\)?[ \\-]?(\\(0\\))|0)((20[7,8]{1}\\)?[ \-]?[1-9]{1}[0-9]{2}[ \\-]?[0-9]{4})|([1-8]{1}[0-9]{3}\\)?[ \\-]?[1-9]{1}[0-9]{2}[ \\-]?[0-9]{3}))\\s*";
var re = new RegExp(regexp);
return 开发者_开发知识库this.optional(element) || re.test(value);
}
else {
return true;
}
},
"Please check your input."
);
And this adding the rule to my input:
$("#<%=txtTelephoneNumber.ID %>").rules("add", {
required: false,
ukphonenumber: true,
messages: {
required: "Please enter your telephone number",
ukphonenumber: "Your telephone number is invalid"
}
});
I also emailed the developer of the library and got this response: Which solved the problem.
is there any reason to use required:false instead of required:true? If the field isn't required, leave out the required rule instead of setting it to false.
You could also try and leave out the call to this.optional(element) within your custom method.
Regards Jörn
this works:
return (this.optional(element) != false) || re.test(value);
Dependency mismatch with Jquery Validate plugin
精彩评论