1

I have multiple asp mvc checkboxes on a form, all with different names, as they are using model binding. I want to be able to use jQuery validation to make sure at least ONE checkbox is checked. But jQuery validation works based on the name of a field, and the names need to be unique for the checkboxes for model binding. How would I use jQuery validation with an asp mvc checkbox group?

tereško
  • 58,060
  • 25
  • 98
  • 150
John Edwards
  • 1,536
  • 3
  • 14
  • 33
  • Duplicate: http://stackoverflow.com/questions/5662589/unobtrusive-mvc3-validating-group-of-checkboxes – Maxim Zhukov Jul 08 '13 at 17:21
  • The only thing I don't see in that solution is how to NOT use an array. I have a list of separate checkboxes with unique names, so I don't know if this will work. – John Edwards Jul 08 '13 at 21:03
  • I would create my own custom attribute server side and and plug in the client-side validation - see this [question](http://stackoverflow.com/questions/4747184/perform-client-side-validation-for-custom-attribute). That way you leverage the build-in validation framework on both sides. – James Jul 09 '13 at 13:55
  • possible duplicate of [jquery: validate form with multiple checkboxes -- at least one must be checked](http://stackoverflow.com/questions/1535187/jquery-validate-form-with-multiple-checkboxes-at-least-one-must-be-checked) – Anton Jul 09 '13 at 13:59

1 Answers1

0

There are a couple ways to do this. Remember, jQuery can select items or a set of items based on css class or input type, not just by name.

If these are the only checkboxes on the page, you can go:

var allboxes = $('input[type="checkbox"]');
if(allboxes.is(":checked"))
{
    //....Do something...
}

There's also an example on the jQuery Checked Selector page that will tell you how to get a count of how many are checked.

Sifford
  • 185
  • 2
  • 9