Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove error class using jQuery Validate?

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;
    }
});

UPDATE:

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/

like image 348
Michael Avatar asked Nov 14 '22 12:11

Michael


1 Answers

Combined the logic.

http://jsfiddle.net/qK3SC/13/

like image 114
Michael Avatar answered Dec 26 '22 14:12

Michael