I have a form that adds an error class to the parent div for a set of checkboxes using the jQuery Validate plugin. However, when a checkbox is selected, the error class is removed but the CSS styling remains. Any ideas what I'm doing wrong?
Example: http://jsfiddle.net/qK3SC/
HTML:
<form name="itemForm" id="itemForm" method="post">
<div id="boxes">
<input type="checkbox" id="check1" class="require-one" value="1" /> Item #1
<input type="checkbox" id="check2" class="require-one" value="2" /> Item #2
</div>
<div id="freetext">
<input type="text" class="required" />
</div>
<input type="submit" />
</form>
Javascript:
$.validator.addMethod('require-one', function(value) {
return $('.require-one:checked').size() > 0;
}, 'Please check at least one box.');
var checkboxes = $('.require-one');
var checkbox_names = $.map(checkboxes, function(e, i) {
return $(e).attr("name")
}).join(" ");
$("#itemForm").validate({
groups: {
checks: checkbox_names
},
errorPlacement: function(error, element) {
if (element.is(':checkbox')) {
$(element).parent('div').addClass('checkbox-error');
}
return true;
}
});
I was able to use the highlight/unhighlight methods to get the error class to work for the checkboxes, but now the error class for my other elements are not working (i.e. text inputs and dropdowns). See: http://jsfiddle.net/qK3SC/7/
Combined the logic.
http://jsfiddle.net/qK3SC/13/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With