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>
<html>
<head>
    <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;
            }
            else
            {
                btn.disabled = true;
            }

        }
    </script>
</head>
<body>
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" />
</body>
</html>

 

Add a Comment

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