views:

42

answers:

1

I have a group of 12 checkboxes, all with the same name (name="4_RepPeriods"). I am using the Jquery validation plugin and i want the user to select no more or less than two of these check boxes.

If possible I also want the other check boxes to become deactivated (unclickable) after two have been selected.

I appreciate the help! Thanks!

A: 

You can use the built-in validation rules for this:

4_RepPeriods: {
    minlength: 2,
    maxlength: 2
}

Alternatively, you can add a custom method, like this:

$.validator.addMethod('select2RepPeriods',function(v, e) {
   return $('[name="4_RepPeriods"]:checked').length == 2;
}, 'You must select exactly 2 rep periods');

Then on your validation, you can now put only this: select2RepPeriods: true.

Nick Craver
Brilliant, Thank you.
jethomas
Actually sorry, its not working, no matter how many checkboxes I pick it gives the error... using the custom method.
jethomas
@jethomas - Updated the example to work with the current version of the validation plugin, seems the method call changed a bit.
Nick Craver
thanks, works like a charm!
jethomas