jQuery validate - set conditional rules based on user selection
Solution 1:
jQuery Validate actually directly supports this, using dependency expressions.
All you need to do is change your validate options like this:
$('#myform').validate({
rules: {
fieldA: {
required:'#checkA:checked'
}
}
});
That's it!
Solution 2:
You can use a custom validation method. For example:
jQuery.validator.addMethod("checkA", function(value, element) {
return YourValidationCode; // return true if field is ok or should be ignored
}, "Please complete A");
then use it in your rules:
rules: {
....
field_a: {
required: false,
checkA: true
},
....
}
Take a look: http://jqueryvalidation.org/jQuery.validator.addMethod/