4

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

4
  • This is a bit out of topic, but don't forget that on the server side you still have to test the whole result, because it may not have been filtered and you still may get unpredictable things in your request (i.e. request sent by hackers ). Commented Oct 14, 2010 at 12:03
  • I am relatively new to jQuery and selectorsm, but isn't there a way to define more specific jQuery selector for a particular checkbox only instead of all checkboxes in the form? Hope I am not pushing you in the wrong track. Commented Oct 14, 2010 at 12:11
  • What plugin are you using for this? It isn't the standard validation plugin... Commented Oct 14, 2010 at 12:17
  • It's built into jquery tools flowplayer.org/tools Commented Oct 14, 2010 at 12:37

1 Answer 1

5

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

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

1 Comment

replaced link , thoank you

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.