jQuery Validation: validate atleast one checkbox is selected from a list where names are different
I need to validate that atleast one check box is selected from a list of checkboxes using jQuery Validation Plugin. Since the checkboxes are part of a ASP.NET GridView Control, the names of these 开发者_如何学Pythoncheckboxes will be different, which makes setting up the rules for the validation plugin complex since it expects the name for the rule. I have searched around and found the below questions
- Question 1710486
- Question 1136507
I think the first question is a much better solution, but i still feel that it is not the right way to do. does anybody else have any more options for this problem.
How about a complex rule that only makes the item required if no other checkboxes have been checked.
rules: {
'require-one': {
required : {
depends: function(element) {
var allBoxes = $('.require-one');
if (allBoxes.filter(':checked').length == 0) {
if (allBoxes.eq(element).length != 0) {
return true;
}
}
return false;
}
}
}
}
Then you'd apply the require-one
class to each checkbox in the set. The first one would be required if none of the boxes where checked.
精彩评论