72

I have the following HTML form which can have many checkboxes. When the submit button is clicked, I want the user to get a javascript alert to check at least one checkbox if none are checked. Is there an easy way to do this using jQuery?

<form name = "frmTest" id="frmTest">
  <input type="checkbox" value="true" checked="true" name="chk[120]">
  <input type="checkbox" value="true" checked="true" name="chk[128]">
  <input type="checkbox" name="chk[130]">
  <input type="checkbox" name="chk[143]">
  <input type="submit" name="btnsubmit" value="Submit">
</form>
TRiG
  • 9,687
  • 6
  • 54
  • 105
Jake
  • 24,303
  • 27
  • 100
  • 159
  • 5
    You would probably be better off with ``. Note also that the **only** acceptable value for the checked attribute is "checked". `checked="true"` is an error. – Quentin Apr 21 '10 at 16:22

6 Answers6

106
if(jQuery('#frmTest input[type=checkbox]:checked').length) { … }
Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
  • 1
    :checked already returns a boolean, so .length is not needed. true.length and false.length both provide undefined – Jan Mar 21 '14 at 12:30
  • 14
    @Jan — `jQuery('#frmTest input[type=checkbox]:checked')` returns a jQuery object containing all the elements that match the selector. It does not return a boolean. You might be confusing it with `HTMLInputElement.checked`. – Quentin Mar 21 '14 at 12:33
36
$('#frmTest input:checked').length > 0
Matthew Flaschen
  • 268,153
  • 48
  • 509
  • 534
24
$("#frmTest").submit(function(){
    var checked = $("#frmText input:checked").length > 0;
    if (!checked){
        alert("Please check at least one checkbox");
        return false;
    }
});
Jon
  • 5,828
  • 9
  • 36
  • 40
10
$("#show").click(function() {
    var count_checked = $("[name='chk[]']:checked").length; // count the checked rows
        if(count_checked == 0) 
        {
            alert("Please select any record to delete.");
            return false;
        }
        if(count_checked == 1) {
            alert("Record Selected:"+count_checked);

        } else {
            alert("Record Selected:"+count_checked);
          }
});
chithon
  • 195
  • 1
  • 5
  • 15
7

$('#fm_submit').submit(function(e){
    e.preventDefault();
    var ck_box = $('input[type="checkbox"]:checked').length;
    
    // return in firefox or chrome console 
    // the number of checkbox checked
    console.log(ck_box); 

    if(ck_box > 0){
      alert(ck_box);
    } 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name = "frmTest[]" id="fm_submit">
  <input type="checkbox" value="true" checked="true" >
  <input type="checkbox" value="true" checked="true" >
  <input type="checkbox" >
  <input type="checkbox" >
  <input type="submit" id="fm_submit" name="fm_submit" value="Submit">
</form>
<div class="container"></div>
Al.G.
  • 4,175
  • 6
  • 34
  • 55
Domingo
  • 135
  • 1
  • 3
6
$('#frmTest').submit(function(){
    if(!$('#frmTest input[type="checkbox"]').is(':checked')){
      alert("Please check at least one.");
      return false;
    }
});

is(':checked') will return true if at least one or more of the checkboxes are checked.