Using the jQuery Validate plugin, I've got a custom method that ensures at least one out of three checkboxes has been checked (as per this question).
This works fine, except that once the one of the checkboxes has been checked, the error container doesn't disappear (even though the error messages do disappear and the form will submit successfully). The error container remains stuck on 'display: block' rather than reverting back to 'display: none'.
Here's a JSFiddle which demonstrates this (it's a simplified version of my form). If one out of three checkboxes is checked, the error container remains in place even though there's no error messages. If all three checkboxes are checked, the error message container disappears. So I'd appreciate any ideas on how to make the error container disappear after just one of the checkboxes has been checked, as per the custom method, thanks.
Here's my HTML:
<form class="my_form" method="post" action="#">
<div class="error-container">
<ul>
</ul>
</div><!--error-container-->
<p><label>My checkbox 1</label>
<input class="my_checkbox_group" id="my_checkbox_1"
name="my_checkbox_1[]" type="checkbox" value="Yes" /></p>
<p><label>My checkbox 2</label>
<input class="my_checkbox_group" id="my_checkbox_2"
name="my_checkbox_2[]" type="checkbox" value="Yes" /></p>
<p><label>My checkbox 3</label>
<input class="my_checkbox_group" id="my_checkbox_3"
name="my_checkbox_3[]" type="checkbox" value="Yes" /></p>
<input type="submit" value="Submit" />
</form>
Here's my CSS:
input.error { background: #fdf3f3; border: 1px solid #ff3333; color: #ff3333; }
.error-container { background: #fdf3f3; border: 1px solid #ff3333; clear: both; display: none; overflow: auto; padding: 10px; }
.error-container ul { margin-bottom: 0; }
.error-container label { float: none; font-weight: normal!important; width: auto; }
Here's my JS:
$(document).ready(function () {
$.validator.addMethod("checkboxrule", function (value, element) {
return ($('#my_checkbox_1').is(':checked') || $('#my_checkbox_2').is(':checked') || $('#my_checkbox_3').is(':checked'))
}, "Select at least one of these three");
$('.my_form').validate({ // initialize the plugin
errorContainer: ".error-container",
errorLabelContainer: ".error-container ul",
wrapper: "li",
focusInvalid: false,
ignore: "",
groups: {
somename: "my_checkbox_1[] my_checkbox_2[] my_checkbox_3[]"
},
rules: {
'my_checkbox_1[]': {
checkboxrule: true
},
'my_checkbox_2[]': {
checkboxrule: true
},
'my_checkbox_3[]': {
checkboxrule: true
}
},
submitHandler: function (form) { // for demo
alert('valid form submitted'); // for demo
return false; // for demo
}
});
});