disable form posting using javascript

Sometimes we do not want user to submit form unless some conditions are met. In the following example, the user will be able submit form only if (s)he clicks on “I agree” checkbox.

<!DOCTYPE html>
    <title>Untitled document</title>
  <meta charset="utf-8" />

    <script language="javascript" type="text/javascript">
        function enableButton()
            var chk = document.getElementById("chkagree");
            var btn = document.getElementById("btnsubmit");

            if(chk.checked == true)
                btn.disabled = false;
                btn.disabled = true;

I agree to the terms and conditions 
    <input id="chkagree" name="chkagree" 
           type="checkbox" onclick="enableButton();" />
    <br />
<input id="btnsubmit" name="btnsubmit" type="submit" 
       value="Submit" disabled="disabled" />


Add a Comment

Your email address will not be published. Required fields are marked *