Submit Form if At Least One Checkbox is Checked


Demo Download

Today in this tutorial I will going to explain how to restrict form submission if at least one checkbox is not checked, when a user press submit button an alert will appear to check at least one checkbox if at least one checkbox is not checked it means form will not be submitted, User will be required to check at least one checkbox to submit form.

Note: Don’t forget to include the jQuery library in the header or footer of your web page.

HTML

<form method="post" action="">
<input type="checkbox" name="cricket" value="cricket" />Cricket<br/>
<input type="checkbox" name="football" value="football" />Football<br/>
<input type="checkbox" name="hockey" value="hockey" />Hockey<br/><br/>
<input type="submit" name="submit" value="Submit" />
</form>

jQuery

$(document).ready(function(){
    $("form").submit(function(){
		if ($('input:checkbox').filter(':checked').length < 1){
        alert("Check at least one Game!");
		return false;
		}
    });
});

If you found this tutorial helpful so share it with your friends, developer groups and leave your comment.

Facebook Official Page: All PHP Tricks

Twitter Official Page: All PHP Tricks

Article By
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.
  1. It’s work bro (all other websites code is not work) but your code is worked your code is too good.
    Thankyou bro.

  2. Can this script be adjusted to check two sets of check boxes to make sure that at least one checkbox is checked in each set of check boxes?

    Checkbox group 1:
    _option 1
    _option 2
    _option 3

    Checkbox group 2:
    _option 1
    _option 2
    _option 3

    At least one check must be checked in each group above. Is this possible?

  3. Javed, I tried to apply this to my form. I also use a privacy checkbox which is required. But can I specify the jQuery with a class?

  4. It’s really very helpful. I have tried this stuff more than 2 weeks. lastly found your page. Now the problem solved.

  5. A whole day wasted on stack exchange trying out various people’s “suggestions”…. just about to give up, and FINALLY found your page. So simple. If you were here I would buy you a drink!

Leave a Reply

Your email address will not be published. Required fields are marked *