Disable Form Submit Until Required

Sometimes you might want the submit button on a form disabled until the user has filled in mandatory checkboxes. A good example of this would be if you had terms and conditions that needed to be read/agreed to before commencing with the form submission. This would need to be checked server-side as well of course to be absolutely certain.

Anyway, on to the script. There is nothing complicated about this at all. We simply disable the submit button on page load and then test the “checked” property of each checkbox by calling a simple function.

The Javascript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<script type="text/javascript">
window.onload = function() {
    document.getElementById("submit").disabled = true;
    document.getElementById("understand1").onclick = checkIt;
    document.getElementById("understand2").onclick = checkIt;
    document.getElementById("understand3").onclick = checkIt;
}
function checkIt() {
    if (document.all||document.getElementById) {
        if (document.getElementById("understand1").checked && document.getElementById("understand2").checked && document.getElementById("understand3").checked) {
            document.getElementById("submit").disabled = false;
        } else {
            document.getElementById("submit").disabled = true;
        }
    }
}
</script>

The HTML

1
2
3
4
5
6
<form>
    <input id="understand1" name="understand1" type="checkbox" />I understand the rules<br />
    <input id="understand2" name="understand2" type="checkbox" />I have read the above and understood it<br />
    <input id="understand3" name="understand3" type="checkbox" />I've told you, I understand!<br />
    <input id="submit" type="Submit" value="Let Me Submit This Form!" />
</form>

You can see an example here.

Feed IconFollow me on Twitter





One Response to “Disable Form Submit Until Required”

  1. Why does the script only recognize html button id. It does not recognize the id in an ASP button.

    This button works:

    This button does not:

    Thanks for sharing!

    Ricardo

Leave a Comment







XHTML: You can use the following tags in your comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">