Validate--dropdown menu lists--no selection made

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ammweb
    New Member
    • Dec 2006
    • 6

    Validate--dropdown menu lists--no selection made

    How would you validate a page that had several drop down menus. When the submit button is submitted, and NO selection has been made?

    See code below:
    [HTML]<html>
    <head>

    </head>
    <body>

    <table width="100%" height="500px" border="0" cellpadding="0" cellspacing="0" >
    <tr>
    <td height="600px" valign="top" bgcolor="#F5F0D D"><table width="100%" height="452" border="1" bgcolor="#BFD5E A">
    <form action="remcard queue.asp" method="post">
    <input name="subject" type="hidden" value="AMM | Remembrance Card Package Order" />
    <input name="MAILCODE" type="hidden" value="XXXX" />
    <tr>
    <td height="66" colspan="3"><di v align="center"> <font color="#006633" size="5">Rememb rance Card Package Selection</font></div></td>
    </tr>
    <tr>
    <td width="22%" height="66"><di v align="center"> <img src="images/cards/1111.jpg" alt="Associatio n Standard Remembrance Card Package" border="0"><br />
    <span class="smallfon t">Click to view details</span></div></td>
    <td width="58%" class="surveysm all"><p><b><fon t color="#006633" >Association Standard Remembrance Card Package</font><font color="#006600" >(One of each of the cards shown here)<br />
    </font></b><span class="surveyfo nt"> X1111</span> </p> </td>
    <td width="20%" class="surveysm all"><div align="right">

    <select name="X1111" id="X1111">
    <option value="0">0</option>
    <option value="1">1</option>
    </select></div></td>
    </tr>
    <tr>
    <td height="58"><di v align="center"> <a href="http://www.amm.org/cards/cdpkg/d011.asp" target="_NewWin "><img src="images/cards/D011.jpg" alt="Baby Remembrance" width="30" height="45" border="0"><br />
    <span class="smallfon t">Click to view details</span></a></div></td>
    <td class="surveysm all"><p><b><fon t color="#006633" >Baby Remembrance Folder</font></b><br />
    <span class="surveyfo nt">D011</span> </td>
    <td class="surveysm all"><div align="right">

    <select name="D011" id="D011">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select></div></td>
    </tr>
    <tr>
    <td><div align="center"> <a href="http://www.amm.org/cards/cdpkg/dfp006.asp" target="_NewWin "><img src="images/cards/dfp006.jpg" alt="Family Prayer" width="32" height="45" border="0" /></a><br />
    <a href="http://www.amm.org/cards/cdpkg/dfp006.asp" target="_NewWin "><span class="smallfon t">Click to view details</span></a></div></td>
    <td><p><span class="surveysm all"><b><font color="#006633" >Family Prayer Folder
    </font></b><br />
    </span><span class="surveyfo nt">DFP006</span>
    </td>
    <td><div align="right">

    <select name="DFP006" id="DFP006">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select></div></td>
    </tr>
    <tr>
    <td><div align="center"> <a href="http://www.amm.org/cards/cdpkg/dh215.asp" target="_NewWin "><img src="images/cards/DH215.jpg" alt="Healing Remembrance" width="30" height="41" border="0" /></a><br />
    <a href="http://www.amm.org/cards/cdpkg/dh215.asp" target="_NewWin "><span class="smallfon t">Click to view details</span></a></div></td>
    <td><span class="surveysm all"><b><font color="#006633" >Healing Folder</font></b><br />
    </span><span class="surveyfo nt">DH215</span> </td>
    <td><div align="right">

    <select name="DH215" id="DH215">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select></div></td>
    </tr>
    <tr>
    <td><div align="center"> <a href="http://www.amm.org/cards/cdpkg/dh005.asp" target="_NewWin "><img src="images/cards/dh005.jpg" alt="Healing Remembrance" width="31" height="45" border="0" /></a><br />
    <a href="http://www.amm.org/cards/cdpkg/dh005.asp" target="_NewWin "><span class="smallfon t">Click to view details</span></a></div></td>
    <td><span class="surveysm all"><b><font color="#006633" >Healing Folder</font></b><br />
    </span><span class="surveyfo nt">DH005</span> </td>
    <td><div align="right">

    <select name="DH005" id="DH005">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select></div></td>
    </tr>
    <tr>
    <td><div align="center"> <a href="http://www.amm.org/cards/cdpkg/dh106.asp" target="_NewWin "><img src="images/cards/dh106.jpg" alt="Healing Remembrance" width="30" height="42" border="0" /></a><br />
    <a href="http://www.amm.org/cards/cdpkg/dh106.asp" target="_NewWin "><span class="smallfon t">Click to view details</span></a></div></td>
    <td><span class="surveysm all"><b><font color="#006633" >Healing Folder</font></b><br />
    </span><span class="surveyfo nt">DH106</span> </td>
    <td><div align="right">

    <select name="DH106" id="DH106">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select></div></td>
    </tr>
    <tr>
    <td><div align="center"> <a href="http://www.amm.org/cards/cdpkg/d3106.asp" target="_NewWin "><img src="images/cards/d3106.jpg" alt="Memorial Remembrance" width="32" height="45" border="0" /></a><br />
    <a href="http://www.amm.org/cards/cdpkg/d3106.asp" target="_NewWin "><span class="smallfon t">Click to view details</span></a></div></td>
    <td><span class="surveysm all"><b><font color="#006633" >Memorial Remembrance Folder</font></b><br />
    </span><span class="surveyfo nt">D3106</span> </td>
    <td><div align="right">

    <select name="D3106" id="D3106">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select></div></td>
    </tr>
    <tr bgcolor="f5f0dd ">
    <td colspan="4"><di v align="center">
    <p>&nbsp; </p>

    <input type="submit" name="button" id="button" value="Create Your Card Package"/>
    </div></td>
    </tr>
    </form>
    </table>
    </td>
    </tr>
    </table>

    </body>
    </html>[/HTML]
    Last edited by gits; Jun 23 '08, 09:11 PM. Reason: added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Add an onsubmit event handler to the form:
    [html]<form ... onsubmit="retur n validate()">[/html]In your validate() function, check that the selectedIndex of each is not 0. If you need only one selection, if even one has a selectedIndex not equal to 0 or value neq. 0, then return true.

    Comment

    • ammweb
      New Member
      • Dec 2006
      • 6

      #3
      Originally posted by acoder
      Add an onsubmit event handler to the form:
      [html]<form ... onsubmit="retur n validate()">[/html]In your validate() function, check that the selectedIndex of each is not 0. If you need only one selection, if even one has a selectedIndex not equal to 0 or value neq. 0, then return true.

      I first tried one dropdown menu list and used your suggestion above, and it worked, but when I tried to added a second dropdown and use the OR operator, it didn't work. So my question is: How do you validate more than one dropdown menu list?

      Below the script that didn't work using the OR:
      Code:
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>Untitled Document</title>
       <script LANGUAGE="JavaScript">
      <!--
      function ValidateForm(form){
      ErrorText= "";
      if ( form.age.selectedIndex == 0 ) || (form.card.selectedIndex == 0)
      { alert ( "Please make a selection." ); return false; }
      
      if (ErrorText= "") 
      { form.submit() }
      
      	}
      -->
      </script>
      
      </head>
      
      <body>
      <br><form name="feedback" action="mailto:ammwebmaster@amm.org" method=post>
          <p>Your Age:    </p>
          <p>
              <select name="age"> 
                  <option value="">Please Select an Option:</option> 
                  <option value="0-18 years">0-18 years</option> 
                  <option value="18-30 years">18-30 years</option> 
                  <option value="30-45 years">30-45 years</option> 
                  <option value="45-60 years">45-60 years</option> 
                  <option value="60+ years">60+ years</option>
                      </select>
          </p>
          <P>
          <select name="card">
              <option value="0">0</option>
              <option value="1">1</option>
              <option value="2">2</option>
          
          </select>
          <p>
              <input type="button" name="SubmitButton" value="Submit" onClick="ValidateForm(this.form)">
              <input type="reset" value="Reset">
              </p>
      </form>
      </body>
      
      </html>

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        There are two problems. You're not changing errorText, so checking for errorText doesn't make sense. Secondly, you're actually setting errorText in the if-statement rather than comparing. Use double-equals for comparison. Having said that, however, you don't need to check for the value of errorText, since if there's an error, you'll already have returned from the function with return false.

        Comment

        • ammweb
          New Member
          • Dec 2006
          • 6

          #5
          Originally posted by acoder
          There are two problems. You're not changing errorText, so checking for errorText doesn't make sense. Secondly, you're actually setting errorText in the if-statement rather than comparing. Use double-equals for comparison. Having said that, however, you don't need to check for the value of errorText, since if there's an error, you'll already have returned from the function with return false.
          I'm sorry but I don't understand! The Code below works now but there is only one drop down list. How do I get the other drop down list to be validated too. Viewer needs to have a selection from one drop down list or the other or receive an Error message--"please make your selection"
          Code:
           <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
          <title>Untitled Document</title>
           <script LANGUAGE="JavaScript">
          <!--
          function ValidateForm(form){
          if (form.age.selectedIndex == 0)
          { alert ( "Please make a selection." )
              ; return false; }
          	}
          -->
          </script>
          </head>
          <body>
          <br><form name="feedback" action="mailto:ammwebmaster@amm.org" method=post>
              <p>Your Age:    </p>
              <p>
                  <select name="age"> 
                      <option value="">Please Select an Age Option:</option> 
                      <option value="0-18 years">0-18 years</option> 
                      <option value="18-30 years">18-30 years</option> 
                      <option value="30-45 years">30-45 years</option> 
                      <option value="45-60 years">45-60 years</option> 
                      <option value="60+ years">60+ years</option>
                          </select>
              </p>
              <P>
             Select the QTY of cards:
              <select name="card">
                  <option value="0">0</option>
                  <option value="1">1</option>
                  <option value="2">2</option>
              </select>
              <p>
                  <input type="button" name="SubmitButton" value="Submit" onClick="ValidateForm(this.form)">
                  <input type="reset" value="Reset">
                  </p>
          </form>
          </body>
          </html>
          can you show me an example of the script you use?

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Oh, I see the error in your previous code. Change this:
            Code:
            if ( form.age.selectedIndex == 0 ) || (form.card.selectedIndex == 0)
            { alert ( "Please make a selection." ); return false; }
            to
            [code=javascript]if ((form.age.sele ctedIndex == 0 ) || (form.card.sele ctedIndex == 0))
            {
            alert ( "Please make a selection." );
            return false;
            }[/CODE]Note the extra brackets/parentheses. Also note that the language attribute of the script tag is deprecated. Use the type attribute instead:
            [html]<script type="text/javascript">[/html]

            Comment

            • ammweb
              New Member
              • Dec 2006
              • 6

              #7
              Originally posted by acoder
              Oh, I see the error in your previous code. Change this:
              Code:
              if ( form.age.selectedIndex == 0 ) || (form.card.selectedIndex == 0)
              { alert ( "Please make a selection." ); return false; }
              to
              [code=javascript]if ((form.age.sele ctedIndex == 0 ) || (form.card.sele ctedIndex == 0))
              {
              alert ( "Please make a selection." );
              return false;
              }[/CODE]Note the extra brackets/parentheses. Also note that the language attribute of the script tag is deprecated. Use the type attribute instead:
              [html]<script type="text/javascript">[/html]
              Thank YOU!
              I had to put && in in place of the || but I got it to work. Code is below.
              How would you get a page that had 7 dropdown menus
              to be validate? So the viewer has to at least select one
              dropdown before hitting the submit button??? Can you put && over and over again?


              Code:
              <script type="text/javascript">
              <!--
              function ValidateForm(form){
                    if ((form.age.selectedIndex == "" ) && (form.card.selectedIndex == 0))
                       {
                           alert ( "Please make a selection." );
                           return false;
                       }
              	 else 
              		 {
              	 	alert ("You have made your selection.");
                 	   return true;
              			}
              		}
              -->
              </script>
              Thanks again for all your help. I have learned a lot with this..
              Last edited by acoder; Jun 24 '08, 08:31 PM. Reason: Added code tags

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Of course, you're checking if all of them are not selected, so it would be &&, not ||
                Originally posted by ammweb
                How would you get a page that had 7 dropdown menus
                to be validate? So the viewer has to at least select one
                dropdown before hitting the submit button??? Can you put && over and over again?
                You could, but it might make more sense to use getElementsByTa gName("select") and looping over the elements, so that if you add more drop-down elements, you don't need to change the code, e.g.
                [code=javascript]var selects = form.getElement sByTagName("sel ect");
                var anySelected = false;
                for (i = 0; i < selects.length; i++) {
                if (selects[i].selectedIndex != 0) {
                anySelected = true;
                break;
                }
                }
                if (!anySelected) {
                // alert error
                return false;
                }[/code]

                Comment

                Working...