jQuery validation: Indicate that at least one element in a group is required
I had a need today to indicate that at least one of a set of input fields was required. I was hoping there was a direct way to do this in the jQuery validation plugin; while the method isn't quite as straightforward as I was wishing for, it's still fairly simple.
To start with, I put class="required_group" on each of the elements in the group. Then, I added a custom validation method:
jQuery.validator.addMethod('required_group', function(val, el) {
var $module = $(el).parents('div.panel');
return $module.find('.required_group:filled').length;
});jQuery.validator.addClassRules('required_group', {
'required_group' : true
});jQuery.validator.messages.required_group = 'Please fill out at least one of these fields.';
