Submit Form if At Least One Checkbox is Checked
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
It’s work bro (all other websites code is not work) but your code is worked your code is too good.
Thankyou bro.
Thanks for the appreciation, glad to hear that you found it helpful.
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?
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?
Yes John, you can use jQuery with a class or ID.
It’s really very helpful. I have tried this stuff more than 2 weeks. lastly found your page. Now the problem solved.
Thanks R.Kalai for liking it.
Thanks bro, very well explained
You welcome 🙂
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!
I am glad that you found it helpful.
@Steve You are right steve. Same here
Thanks Muhammad Aftab, i am glad that you people found it helpful.
it’s helpfull.
Thanks Jack
nice tutorial…thanks
Thanks Amol 😉