Web Analytics Made Easy -
StatCounter need more efficient code for form validation - CodingForum

Announcement

Collapse
No announcement yet.

need more efficient code for form validation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • need more efficient code for form validation

    Very little experience with js as you will soon tell. I need more efficient code in form validation to make sure at least one radio button is selected on form--has five sections of three buttons each. I need to make sure at least one button is selected. Here is what I'm trying to optimize:

    if ((document.forms[0].yellowform[0].checked == false) &&
    (document.forms[0].yellowform[1].checked == false) &&
    (document.forms[0].yellowform[2].checked == false) &&
    (document.forms[0].blueform[0].checked == false) &&
    (document.forms[0].blueform[1].checked == false) &&
    (document.forms[0].blueform[2].checked == false) &&
    (document.forms[0].greenform[0].checked == false) &&
    (document.forms[0].greenform[1].checked == false) &&
    (document.forms[0].greenform[2].checked == false) &&
    (document.forms[0].scriptpad[0].checked == false) &&
    (document.forms[0].scriptpad[1].checked == false) &&
    (document.forms[0].scriptpad[2].checked == false) &&
    (document.forms[0].auxlabel[0].checked == false) &&
    (document.forms[0].auxlabel[1].checked == false) &&
    (document.forms[0].auxlabel[2].checked == false) &&
    (document.forms[0].alertlabel[0].checked == false) &&
    (document.forms[0].alertlabel[1].checked == false) &&
    (document.forms[0].alertlabel[2].checked == false))

    { alert ("Please select form");}

  • #2
    declare a global variable:

    var gotone=false;

    then add an onclick to each radio:
    onclick="gotone=true";

    Then all you need for validation is:

    if(!gotone) alert("please selectone");
    100% standards compliant code is 100% correct 100% of the time.

    Cd&

    Comment


    • #3
      Something like this?
      Code:
      <html>
       <head>
        <script type="text/javascript">
         function validate()
         {
             var form = document.forms[0];
      
             for (v in form)
             {
                 var node = form[v];
      
                 if (node &&
                     node.tagName &&
                     (node.tagName.toLowerCase() == "input") &&
                     (node.getAttribute("type").toLowerCase() == "checkbox") &&
                     (node.checked))
                 {
                     alert("Submitting Form!");
                     return true;
                 }
             }
      
             alert("Please select a checkbox");
                 
             return false;
         }
        </script>
       </head>
       <body>
        <form onsubmit="return validate();">
         <input type="checkbox" name="a1" />
         <input type="checkbox" name="a2" />
         <input type="checkbox" name="a3" />
         <input type="checkbox" name="a4" />
         <input type="checkbox" name="a5" />
         <input type="checkbox" name="a6" />
         <input type="checkbox" name="a7" />
         <input type="checkbox" name="a8" />
         <input type="checkbox" name="a9" />
         <input type="checkbox" name="a10" />
         <input type="submit" value="test" />
       </body>
      </html>
      ScriptingMagic.com

      Comment


      • #4
        Here are a couple drop in form validation scripts that were recently posted:

        http://www.codingforum.net/showthrea...ewpost&t=64683

        Basscyst
        Helping to build a bigger box. - Adam Matthews

        Comment

        Working...
        X