ss888
ss888

Reputation: 1748

jQuery checkbox validation

I'm using the jQuery code below to validate a form. Now it all works great but I would like for the checkbox validation to use the .validator rather than its current alert, but when I add required="required" to the checkbox input boxes, I get messages for all of the checkboxes, whereas I only need one.

$("#request-form").validator({
    message: '<div><em/></div>',
    position: 'top left',
    offset: [3, 40]
});

$("#see").overlay({mask: '#999', fixed: false}).bind("onBeforeClose", function(e) {
    $(".error").hide();
});

$('#request-form').submit(function(){
    if(!$('#request-form input[type="checkbox"]').is(':checked')){
        alert("Please check at least one.");
        return false;
    }
});

Hope this makes sense. Thanks in advance. S

Upvotes: 4

Views: 33081

Answers (1)

mcgrailm
mcgrailm

Reputation: 17640

Here is some code that will work with the JQuery Validation Plugin. I'm not sure if this will work with what you're using because I've never heard of it.

$("#request-form").validate({
    rules: {
        checkbox: { 
        required: 'input[type="checkbox"]:checked',
        minlength: $('input[type="checkbox"]').length()
        }
    },
    messages: {
        checkbox: "Please check at least one.",
    }
});

HTH

Upvotes: 5

Related Questions