Accept Terms & Condition Script in JS

On any site, while registering we accept terms and conditions. In this tutorial we are going to see how to implement accept terms and condition logic in your webpage using JavaScript.


JavaScript Code :

First of all we need to put following JS code in the head section of the HTML.

<script language=JavaScript>
<!--
function validate(form){
  if (form.agree.checked == false ) {
      alert('Please Accept Terms and Conditions....');
	return false;
  } else {
	return true;
  }
}
//-->
</script>

Now we need to call the above JS code on the button click event. Put below form code in the body section of HTML -

<form   action="success.html" 
        method="GET" 
	onsubmit="return validate(this)">
<div class="terms">
	<!-- Your Content -->
	<!-- Your Content -->
</div>
I accept: <input type="checkbox" value="0" name="agree">
<input type="submit" value="Submit">
</form>

Code Explanation :

We have -

Parameter Use
success.html This page will be rendered when user successfully accepts terms and conditions.
validate(this) This method will be called to check whether the check box is checked or not.
In the JS code following statement will provide us the checked value of the Check Box -

form.agree.checked