开发者

jQuery Validation: validate atleast one checkbox is selected from a list where names are different

开发者 https://www.devze.com 2022-12-11 05:34 出处:网络
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 开发

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消