Your best option in this case is to just write your own rule using the .addMethod()
method.
simple example from docs:
jQuery.validator.addMethod("myrule", function(value, element) {
// return 'true' to pass validation or return 'false' to fail validation
return this.optional(element) || /^http:\/\/mycorporatedomain.com/.test(value);
}, "Please specify the correct domain for your documents");
markup to declare this example rule:
rules: {
myfield: {
myrule: true // only passes validation if "http://mycorporatedomain.com" is entered
}
}
simple example from docs using parameters:
jQuery.validator.addMethod("myrule", function(value, element, params) {
// return 'true' to pass validation or return 'false' to fail validation
return this.optional(element) || value == params[0] + params[1];
}, jQuery.validator.format("Please enter the correct value for {0} + {1}"));
markup to declare this example rule:
rules: {
myfield: {
myrule: [5,20] // only passes validation if '25' is entered
}
}
this.optional(element)
in both examples makes the field entry "optional". If you also want the field required
, just remove this part.
You can browse through the additional-methods.js file to see dozens of real working examples of this method.