Please help with a form (javascript)

6 replies
Hi,

Here's the form:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Checklist</title>
<script type="text/javascript">
function disable_enable(){
if (document.all || document.getElementById){
if (document.reqcheck.checkit.disabled==true)
document.reqcheck.checkit.disabled=false
else
document.reqcheck.checkit.disabled=true
}
}
</script>
</head>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
Checklist:
<form action="#" method="post" name="reqcheck">
<ul>
<li>Check if _____ is done <input type="checkbox" name="reqcheck1" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck2" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck3" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck4" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck5" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck6" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck7" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck8" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck9" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck0" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqchecka" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheckb" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheckc" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheckd" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqchecke" onclick="javascript:disable_enable()"></li>
</ul>
<input name="checkit" type="submit" value="Submit" disabled="disabled">
</form>
</BODY>
</HTML>
I want this form to require all boxes be checked, before the submit button can be clicked...

What do I need to modify/add/whatever to get it to work?

The action after the submit button is clicked is not important...

Thanks, and...

Be Well!
ECS Dave
#form #javascript
  • Profile picture of the author sonia2012
    Try this one.
    <script>
    function disable_enable()
    {
    if(document.reqcheck.reqcheck1.value=="")
    return false;
    else if(document.reqcheck.reqcheck2.value=="")
    return false;
    else if(document.reqcheck.reqcheck3.value=="")
    return false;
    else if(document.reqcheck.reqcheck4.value=="")
    return false;
    else if(document.reqcheck.reqcheck5.value=="")
    return false;
    else if(document.reqcheck.reqcheck6.value=="")
    return false;
    else if(document.reqcheck.reqcheck7.value=="")
    return false;
    else if(document.reqcheck.reqcheck8.value=="")
    return false;
    else if(document.reqcheck.reqcheck9.value=="")
    return false;
    else if(document.reqcheck.reqcheck0.value=="")
    return false;
    else if(document.reqcheck.reqchecka.value=="")
    return false;
    else if(document.reqcheck.reqcheckb.value=="")
    return false;
    else if(document.reqcheck.reqcheckc.value=="")
    return false;
    else if(document.reqcheck.reqcheckd.value=="")
    return false;
    else if(document.reqcheck.reqchecke.value=="")
    return FALSE;
    else
    return TRUE;
    }

    function checkIt()
    {
    if(disable_enable()==TRUE)
    {
    document.reqcheck.buttonname.disabled = FALSE;
    }
    else
    {
    document.reqcheck.buttonname.disabled = TRUE;

    }

    }
    </script>
    replace the onclick="javascript:checkIt()"
    {{ DiscussionBoard.errors[3485723].message }}
  • Profile picture of the author SteveJohnson
    For the following to work, you'll need to put an ID on your submit button, i.e.:
    <input type="submit" id="checkit" name="checkit" value="Submit" />

    Code:
    <script type="text/javascript">
        function disable_enable(){
            var inputs = document.getElementsByTagName("input");
            var cbs = [];
            var checked = [];
            for (var i = 0; i < inputs.length; i++) {
              if (inputs[i].type == "checkbox") {
                cbs.push(inputs[i]);
                if (inputs[i].checked) {
                  checked.push(inputs[i]);
                }
              }
            }
            if ( cbs.length == checked.length ) {
                document.getElementById("checkit").disabled = false;
            }
        }
    </script>
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[3487944].message }}
    • Profile picture of the author ECS Dave
      Awesome...

      "Thanks" added to your total

      I appreciate your help, and the time you took to give it.

      Be Well!
      ECS Dave

      P.S. Being as I'm asking for more...
      What would it take to fix it so's if all boxes
      were to be checked, and any one were to get
      unchecked, for the button to be disabled again?
      Now for the "Being as I asked for more" - How much do I owe you?
      {{ DiscussionBoard.errors[3490372].message }}
      • Profile picture of the author SteveJohnson
        Originally Posted by ECS Dave View Post

        What would it take to fix it so's if all boxes were to be checked, and any one were to get unchecked, for the button to be disabled again?
        try this - there's an added line that disables the submit button before the form is examined.

        Code:
        <script type="text/javascript">
            function disable_enable(){
                var inputs = document.getElementsByTagName("input");
                var cbs = [];
                var checked = [];
                document.getElementById("checkit").disabled = true;
                for (var i = 0; i < inputs.length; i++) {
                  if (inputs[i].type == "checkbox") {
                    cbs.push(inputs[i]);
                    if (inputs[i].checked) {
                      checked.push(inputs[i]);
                    }
                  }
                }
                if ( cbs.length == checked.length ) {
                    document.getElementById("checkit").disabled = false;
                }
            }
        </script>
        Now for the "Being as I asked for more" - How much do I owe you?
        pfft. You're welcome.
        Signature

        The 2nd Amendment, 1789 - The Original Homeland Security.

        Gun control means never having to say, "I missed you."

        {{ DiscussionBoard.errors[3493515].message }}

Trending Topics