How to use jquery validate "depends" on rules other than "required"?
jquery, validation
Solution
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/
Problem
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.