78

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>
1
  • 5
    You would probably be better off with <input name="chk[]" value="120">. Note also that the only acceptable value for the checked attribute is "checked". checked="true" is an error. Commented Apr 21, 2010 at 16:22

6 Answers 6

112

Use the :checked pseudo-class to get a list of the checkboxes which are checked, then test the length of the list. Anything other than zero will be a true value.

if(jQuery('#frmTest input[type=checkbox]:checked').length) { … }

You can skip involving jQuery if you like:

document.getElementById('frmTest').addEventListener('submit', (e) => {
  e.preventDefault();
  if (document.querySelectorAll('#frmTest input[type=checkbox]:checked').length) {
    alert("At least one checkbox is checked");
  } else {
    alert("No checkboxes are checked");
  }
});
<form id="frmTest">
  <input type="checkbox">
  <input type="checkbox">
  <input type="checkbox" checked>
  <input type="checkbox">
  <button>Show result</button>
</form>

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

Comments

37
$('#frmTest input:checked').length > 0

3 Comments

$('#frmTest:checkbox').length > 0 would be better to check among a certain number of checkboxes.
Why would radio-buttons be better? If the OP wants at least one option selected radio-buttons would limit him to one at most.
@David, I've removed that suggestion.
27
$("#frmTest").submit(function(){
    var checked = $("#frmText input:checked").length > 0;
    if (!checked){
        alert("Please check at least one checkbox");
        return false;
    }
});

Comments

9
$("#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);
          }
});

1 Comment

Doesn't work for me, something changed in how jQuery handles checkbox checked event?
9

$('#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>

Comments

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.

2 Comments

Is it possible for this to check on live checkbox clicks?
Yes, but you would use a different event handler. See this: stackoverflow.com/questions/3442322/…

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.