0

I have the following checkbox

<input type="checkbox" name="compare[]" value="<?php echo $product_id  ?>" />

Now, I want to let my users select minimum three and maximum three options from the above check box. Breaking of any of the rules would give a message.

Would you please kindly help me how to do that with Jquery?

Thanks in Advance

2
  • 1
    Have you tried anything? Commented Dec 7, 2011 at 1:05
  • Why is the name of the checkbox "compare[]"? Why the need for the brackets? I hope you don't think that by adding brackets to the name of an input that it'll make multiple inputs... Commented Dec 14, 2011 at 2:33

3 Answers 3

2

Here is a 1 solution

var $chkbox = $('input[name=compare]');
$chkbox.click(function (e) {
    if ($chkbox.filter(':checked').length > 3) {
        e.target.checked = false;
    }
});

http://jsfiddle.net/H96nz/

var $chkbox = $('input[type="checkbox"]');

$chkbox.click(function (e) {
    if ($chkbox.filter(':checked').length > 3) {
        e.target.checked = false;
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Trevor for you reply. Your script is not working, may be because my fieldname is-- name="compare[]"
Well, change the name or use a different selector. Either way, this is your solution. You may need to alter some things, but it does work. See my edit.
0

Try this plugin: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

Comments

0

You can loop over your checkbox's using jQuery.each(), checking for checked items and adding checks to a count. Then if the count is not equal to 3 you can output the validation error message.

$(function(){
$("#frm").submit(function(){
    var checkCount = 0;
    var min = 3;
    var max = 3;

    // iterate over checkbox's using jquery each()
    // use jquery's attribute contains selector ( http://api.jquery.com/attribute-contains-selector/ )
    // to grab the compare portion of the checkbox field name
    $("input[name*='compare']").each(function(k, v) {

    // increment check count when a checked checkbox is found
    if($(this).is(":checked"))
        checkCount++;
    });

    //validate checkbox count against predefined range
    if(checkCount < min || checkCount > max)
    {
        console.log("Checkbox Validation Failed");
        return false;
    }
    else
        console.log("Checkbox Validation Passed");

});
});

2 Comments

Thank you for your reply. I followed the link that you gave and I tried to understand the example given there, but as it turned out I have no clue what they are saying . Would you please kindly explain me how to do this when your fieldname is like-- fieldname[] ?? Thanks :)
Code example with comments now included

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.