Reputation: 5576
Every example of "depends" I can find on the web uses the "required" rule. Clearly this is not sufficient. How do I use the "depends" clause with, for example, a "regex" rule?
I have two radio buttons, if one is selected, I regex-validate a textbox. If the other is selected, I don't care what is in the textbox.
Upvotes: 8
Views: 17138
Reputation: 5731
You can also directly use function() in rules as below:
rules: {
"field_name": {
required: function () {
return $('#len').is(':checked');
},
},
}
......
......
Upvotes: 0
Reputation: 21226
The 2nd example in the documentation uses email:
$(".selector").validate({
rules: {
contact: {
required: true,
email: {
depends: function(element) {
return $("#contactform_email:checked")
}
}
}
}
});
In summary, that rule is saying "contact is required and it must be an email address if #contactform_email
checkbox is checked".
So if you wanted to do that with a regex (or any parameter-requiring rule method), it looks like this:
minlength: {
param:6,
depends: function (element) {
return $('#len').is(':checked');
}
}
Which says "The min length is 6 but only when the #len
checkbox is checked".
See it in action here: http://jsfiddle.net/ryleyb/8Nsm3/
Upvotes: 18