I'm using the jQuery Validation Plugin on a form and I'm having problems styling a checkbox group. The plugin adds a class (.error) to the invalid input which I'm using to change the colour of the input.
The problem is that when you validate on a group of checkboxes it only adds the class to the first checkbox in the group. I would like it to behave in the same way radio inputs do. When none are selected they all get the error class, then when one is selected they all get the valid class.
DEMO: reduced test case with a checkbox input group and radio input group.
Submitting the form in the demo you can see that only the first checkbox gets the error class but all radio inputs get it.
// Example html
<form action="#" id="form" method="post">
<ul>
<li>
<input type="checkbox" name="checkbox[]" id="checkbox1">
<label for="checkbox1">Mike</lable>
</li>
<li>
<input type="checkbox" name="checkbox[]" id="checkbox2">
<label for="checkbox2">November</lable>
</li>
<li>
<input type="checkbox" name="checkbox[]" id="checkbox3">
<label for="checkbox3">Oscar</lable>
</li>
</ul>
<input type="submit" value="submit">
</form>
// Validation Plugin Config
$(document).ready(function() {
$("#form").validate({
rules: {
"checkbox[]": {
required: true,
minlength: 1
},
radio: "required",
},
validClass: "js-valid",
errorLabelContainer: ".js-errors",
errorElement: "li",
messages: {
"checkbox[]": "Please select at least one checkbox",
radio: "Please choose from the Radio Group",
},
});
});
I found this suggestion in the jQuery forums but when I tried doing that the error class seemed to be applied to all the checkboxes but selecting one didn't remove it.