|
|
Validation (checking ) of checkbox selection on submit of FormCheck 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. The difference in check box and period button is the user can't select more than one option in case of period buttons. In checkbox the user can give more than one option. For example in a newsletter script you can ask the visitor to select their interested areas. Here the user can give one or more than one are as the area of interest. So all options are to be stored or covered for the user. This way we can get more than one option for a choice. Here we will try with one checkbox and in another tutorial we will consider more than one checkbox.
In signup forms you will see one checkbox asking you to read the term and condition 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.
Here is the demo for checkbox validation
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 (!document.form1.sex[0].checked && !document.form1.sex[1].checked){
alert("Please Select Sex");
return false;}
if(!document.form1.agree.checked){alert("Please check the terms and conditions");
return false; }
return true;
}
</script>
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>
</table></form>
Found anything wrong or wants to improve the code by adding more features? Post your short comment here or use the Forum
| |
| | | shashi | 24-06-2010 |
|---|
| thanks for such a small and beautiful script. It worked exactly the way i want. | | Lesley Olley | 08-09-2011 |
|---|
| Could I add another range of radio button with a different criteria into the same form? | | vamshi | 22-10-2011 |
|---|
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. | | Senthilnathan.V | 02-04-2012 |
|---|
| Can i have a script which will embed with regular expression in C# | | Richa | 23-08-2012 |
|---|
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..
| | jayashree | 10-10-2012 |
|---|
| How to validate list in javascript.Please send it as soon as possible |
|
|
|
|
|
|