$("#myForm").validate({
rules: {
myTextbox: {
required: "#myCheckbox:checked"
}
}
});
This tells 'required' rule that it should be used only when myCheckbox is checked. However'required' is the only rule that comes out of box with an option to specify a condition under which it works. What should you do in case you need to make your own rule use dependency-expression?To enable depedency behavior for a custom rule use the following code:
jQuery.validator.addMethod("greaterThanZero", function(value, element, param) {
if ( !this.depend(param, element) )
return "dependency-mismatch";
return this.optional(element) || (parseFloat(value) > 0);
}, "* Amount must be greater than zero");
No comments:
Post a Comment