1

I have some checkboxes, something like this:

<form name="submit-form" name="submit-form">
<label>property1</label>
<input type="checkbox" name="property1"><br>
<label>property2</label>
<input type="checkbox" name="property2"><br>
<label>property3</label>
<input type="checkbox" name="property3"><br>
<label>property4</label>
<input type="checkbox" name="property4"><br>
<label>property5</label>
<input type="checkbox" name="property5"><br>
<label>property6</label>
<input type="checkbox" name="property6"><br>
<input type="submit">
</form>

I would like to validate that atleast one checkbox at a time. plz help me. thanks in advance

Ravneet 'Abid'
  • 87
  • 3
  • 4
  • 7

4 Answers4

8
$("#YourbuttonId").click(function(){
    if($('#YourTableId').find('input[type=checkbox]:checked').length == 0)
    {
        alert('Please select atleast one checkbox');
    }
});

Update:

i saw your code and seems like you are using jquery validation plugin in that case this post will help you

and try giving your checkbox same name which exist in a group

http://forum.jquery.com/topic/check-that-at-least-1-checkbox-is-checked-using-jquery-validate-plugin

rahul
  • 7,573
  • 7
  • 39
  • 53
2

I'd wrap them all in a container with a div just for easier selecting, and then you can use:

if($('#wrapperID input:checked').length > 0) {
    'at least one is checked
}
DA.
  • 39,848
  • 49
  • 150
  • 213
1

try this

$('form').submit(function(e) {
    if($("input:checked").length == 0){
    alert('please checked atleast one');
    }
});​
Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
0

See this demo: http://jsfiddle.net/RGUTv/ OR your specific case demo here: http://jsfiddle.net/J98Ua/

My old response here: validation for at least one checkbox

hope rest fits the needs! :)

code

$(document).ready(function() {
    $('#my-form').submit(function() {
        amIChecked = false;
        $('input[type="checkbox"]').each(function() {
            if (this.checked) {
                amIChecked = true;
            }
        });
        if (amIChecked) {
            alert('atleast one box is checked');
        }
        else {
            alert('please check one checkbox!');
        }
        return false;
    });
});​
Community
  • 1
  • 1
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
  • it not works. I know u solve many of the problems of checkbox validation before this but it not works and I don't want it in alert... – Ravneet 'Abid' Oct 27 '12 at 06:49
  • @Ravneet'Abid' Cool man, lol can you make a demo of your problem of flick the JQ code please? Lemme know what part actually doesn't work. – Tats_innit Oct 27 '12 at 06:50