Reputation: 2238
How to validate the checkboxes in jquery with the minimum limit of selection. i am using the jquery plugin for form validation that validates the field by passing the field name.
jQuery('#myform').validate({
rules: {
checkbox_name: {
required: true
}
});
now how to apply the minimum selection limit in this criteria?
Upvotes: 2
Views: 2515
Reputation: 98728
The required
rule is all that's needed to make at least one checkbox required.
Yours didn't work because you were missing a closing brace within .valdiate()
...
$('#myform').validate({
rules: {
checkbox_name: {
required: true
}
} // <- this bracket was missing
});
Simple DEMO that makes one of the checkboxes from the group required...
DEMO: http://jsfiddle.net/gh96s/
If you want to required that at least X number of checkboxes from the group are selected, use the minlength
rule. (you can also use the maxlength
or rangelength
rules)
$(document).ready(function () {
$('#myform').validate({
rules: {
checkbox_name: {
required: true,
minlength: 2 // at least two checkboxes are required
// maxlength: 4 // less than 5 checkboxes are required
// rangelength: [2,4] // must select 2, 3, or 4 checkboxes
}
}
});
});
Working DEMO: http://jsfiddle.net/gh96s/2/
Upvotes: 2