Validation (checking ) of checkbox selection on submit of Form

Check box is used to get user inputs within the form. A group of checkbox is used or a single check box is used to collect the options of the visitor.
<form  name=f1>
<input type=checkbox name=c1 id=c1 value=Python> Python
<input type=checkbox name=c2 id=c2 value=PHP> PHP
<input type=checkbox name=c3 id=c3 value=JavaScript> JavaScript

<input type=button name=b1 id=b1 value=Submit onClick=my_check()>

<div id=d1>Output here </div>
Three checkboxes for the user to select. Here name attribute of the checkbox is not common. To get the status of any checkbox we can use like this.
var my_c = document.f1.c1.checked;// true if checked, false otherwise
By using getElementById()
var my_c = document.getElementById('c1');
You can also use getElementsByName

In signup forms you will see one checkbox asking you to read the terms and conditions of the site and the user must agree to this by selecting a checkbox. On submit of the form the status of this checkbox is checked and if not checked by the user then alert message is displayed saying the user has to agree to the terms and conditions.

Here is one example of this system. For easy understanding one period button validation is included in the checking also. The user has to select one of the sexes and must agree to terms and condition by checking the checkbox. The JavaScript code is kept within the head tag of the page. See the validation

<script type="text/javascript"> 
function validate(form) { 
// Checking if at least one period button is selected. Or not. 
if (![0].checked && ![1].checked){ 

alert("Please Select Sex"); 
return false;}

if(!document.form1.agree.checked){alert("Please check the terms and conditions");
 return false; } 

return true;
To display the period button and check box see the code below.
<table border='0' width='50%' cellspacing='0' cellpadding='0' >
<form name=form1 method=post action=action_page.php onsubmit='return validate(this)'>
<input type=hidden name=todo value=post>

<tr bgcolor='#ffffff'><td align=center  ><font face='verdana' size='2'><b>Sex</b>
<input type=radio name=sex value='male'>Male </font><input type=radio name=sex value='female'>
<font face='verdana' size='2'>Female</font></td></tr>

<tr><td align=center  bgcolor='#f1f1f1'><font face='verdana' size='2'>
<input type=checkbox name=agree value='yes'>I agree to terms and conditions  </td></tr>

<tr bgcolor='#ffffff'><td align=center  >
<input type=submit value=Submit> <input type=reset value=Reset></td></tr>


More on CheckBox & PHP


thanks for such a small and beautiful script. It worked exactly the way i want.
Lesley Olley


Could I add another range of radio button with a different criteria into the same form?


I want a script for both the radio buttons in such a way that one text field should be displayed when I select Yes radio button and another text field should be displayed when I select No radio button. Please do the needful and I need it as soon as possible. Thanks in advance, Vamshi.


Can i have a script which will embed with regular expression in C#


I want a html code with a textbox and a button with a fuction to handle onclick event of botton which displays the text written in the textbox on the botton. I shall be lookinf for all our positive respons!! plz post it as soon as possible..


How to validate list in javascript.Please send it as soon as possible

Post your comments , suggestion , error , requirements etc here .

We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2021 All rights reserved worldwide Privacy Policy Disclaimer