5

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/

3
  • Regarding your "update" and jsFiddle #7 above: Using "highlight", you are now applying your green check-box error class to everything including the text box. See: jsfiddle.net/qK3SC/10 Commented Dec 19, 2011 at 20:06
  • 1
    You also created a custom Validator method called require-one but I don't see it being used anyplace. Commented Dec 19, 2011 at 20:26
  • @Sparky672 your last fiddle is close to what i'm looking for. it is difficult to show the validation for textboxes, so ideally, i'd like to turn the text of the checkboxes red and the background of the text input red. If you look at my first fiddle (jsfiddle.net/qK3SC), I am doing that, but the checkbox error class doesn't get removed when one is selected. Any ideas? Commented Dec 20, 2011 at 12:37

2 Answers 2

1

Combined the logic.

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

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried using the success option in your validate method? like

    $("#itemForm").validate({
groups: {
    checks: checkbox_names
},
errorPlacement: function(error, element) {
    if (element.is(':checkbox')) {
        $(element).parent('div').addClass('checkbox-error');

    }
    return true;
},
 success: function(){
       $('.checkbox-error').removeClass('.checkbox-eror');
}
    });

2 Comments

This doesn't seem to be working. jsfiddle.net/qK3SC/6. The error class isn't removed from the parent div (text is still green in example).
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. jsfiddle.net/qK3SC/7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.