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.
DEMO of 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.
<html>
<head>
<title>(Type a title for your page here)</title>
<META NAME="DESCRIPTION" CONTENT=" ">
<META NAME="KEYWORDS" CONTENT=" ">
</head>
<body >
<form name=form1 method=post action=action_page.php onsubmit='return validate(this)'><input type=hidden name=todo value=post>
<div ><b>gender</b><input type=radio name=gender value='male'>Male </font><input type=radio name=gender value='female'>Female</div></div>
<div><div ><input type=checkbox name=agree value='yes'>I agree to terms and conditions </div>
<div ><div id='my_msg'>Submit the form by clicking the button</div></div></div>
<div ><div ><input type=Submit value=Submit> <input type=reset value=Reset></div></div>
</form>
<script type="text/javascript">
document.addEventListener("click", validate);
function validate(form) {
// Checking if at least one period button is selected. Or not.
if (!document.form1.gender[0].checked && !document.form1.gender[1].checked){
//alert("Please Select gender");
document.getElementById('my_msg').innerHTML="Select gender by selecting radio buttons before submitting ";
return false;}
if (!document.form1.agree.checked) {
document.getElementById('my_msg').innerHTML="Agree to Terms & Conditions checkbox before submitting ";
return false; }
document.getElementById('my_msg').innerHTML="Validation is complete.";
return true;
}
</script>
</body>
</html>