Adding dependent validations using jQuery Validate
While validating one of the many forms, I came accross a situation where a form field’s presence was dependent on another DOM element, depends property of jquery-validate plugin came to resue.
rules: {
'contact': {
required: true,
email: {
depends: function(element) {
return $("#contactform_email").is(":checked");
}
},
number: {
depends: function(element) {
return $("#contactform_number").is(":checked");
}
}
}
}In the above example, contact is always required, but email and number are dependent on a radio being checked for contact via email and mobile number respectively.
References: .validate() method