75

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
  • 10,148
  • 7
  • 57
  • 107
Jake
  • 25,479
  • 31
  • 107
  • 168
  • 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

108
if(jQuery('#frmTest input[type=checkbox]:checked').length) { … }
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 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
  • 278,309
  • 50
  • 514
  • 539
26
$("#frmTest").submit(function(){
    var checked = $("#frmText input:checked").length > 0;
    if (!checked){
        alert("Please check at least one checkbox");
        return false;
    }
});
Jon
  • 5,956
  • 10
  • 39
  • 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
10

$('#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,327
  • 6
  • 31
  • 56
Domingo
  • 165
  • 1
  • 4
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.