473,587 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validate--dropdown menu lists--no selection made

6 New Member
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]
Jun 23 '08 #1
7 5533
acoder
16,027 Recognized Expert Moderator MVP
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.
Jun 24 '08 #2
ammweb
6 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  4. <title>Untitled Document</title>
  5.  <script LANGUAGE="JavaScript">
  6. <!--
  7. function ValidateForm(form){
  8. ErrorText= "";
  9. if ( form.age.selectedIndex == 0 ) || (form.card.selectedIndex == 0)
  10. { alert ( "Please make a selection." ); return false; }
  11.  
  12. if (ErrorText= "") 
  13. { form.submit() }
  14.  
  15.     }
  16. -->
  17. </script>
  18.  
  19. </head>
  20.  
  21. <body>
  22. <br><form name="feedback" action="mailto:ammwebmaster@amm.org" method=post>
  23.     <p>Your Age:    </p>
  24.     <p>
  25.         <select name="age"> 
  26.             <option value="">Please Select an Option:</option> 
  27.             <option value="0-18 years">0-18 years</option> 
  28.             <option value="18-30 years">18-30 years</option> 
  29.             <option value="30-45 years">30-45 years</option> 
  30.             <option value="45-60 years">45-60 years</option> 
  31.             <option value="60+ years">60+ years</option>
  32.                 </select>
  33.     </p>
  34.     <P>
  35.     <select name="card">
  36.         <option value="0">0</option>
  37.         <option value="1">1</option>
  38.         <option value="2">2</option>
  39.  
  40.     </select>
  41.     <p>
  42.         <input type="button" name="SubmitButton" value="Submit" onClick="ValidateForm(this.form)">
  43.         <input type="reset" value="Reset">
  44.         </p>
  45. </form>
  46. </body>
  47.  
  48. </html>
  49.  
Jun 24 '08 #3
acoder
16,027 Recognized Expert Moderator MVP
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.
Jun 24 '08 #4
ammweb
6 New Member
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"
Expand|Select|Wrap|Line Numbers
  1.  <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  4. <title>Untitled Document</title>
  5.  <script LANGUAGE="JavaScript">
  6. <!--
  7. function ValidateForm(form){
  8. if (form.age.selectedIndex == 0)
  9. { alert ( "Please make a selection." )
  10.     ; return false; }
  11.     }
  12. -->
  13. </script>
  14. </head>
  15. <body>
  16. <br><form name="feedback" action="mailto:ammwebmaster@amm.org" method=post>
  17.     <p>Your Age:    </p>
  18.     <p>
  19.         <select name="age"> 
  20.             <option value="">Please Select an Age Option:</option> 
  21.             <option value="0-18 years">0-18 years</option> 
  22.             <option value="18-30 years">18-30 years</option> 
  23.             <option value="30-45 years">30-45 years</option> 
  24.             <option value="45-60 years">45-60 years</option> 
  25.             <option value="60+ years">60+ years</option>
  26.                 </select>
  27.     </p>
  28.     <P>
  29.    Select the QTY of cards:
  30.     <select name="card">
  31.         <option value="0">0</option>
  32.         <option value="1">1</option>
  33.         <option value="2">2</option>
  34.     </select>
  35.     <p>
  36.         <input type="button" name="SubmitButton" value="Submit" onClick="ValidateForm(this.form)">
  37.         <input type="reset" value="Reset">
  38.         </p>
  39. </form>
  40. </body>
  41. </html>
  42.  
can you show me an example of the script you use?
Jun 24 '08 #5
acoder
16,027 Recognized Expert Moderator MVP
Oh, I see the error in your previous code. Change this:
Expand|Select|Wrap|Line Numbers
  1. if ( form.age.selectedIndex == 0 ) || (form.card.selectedIndex == 0)
  2. { alert ( "Please make a selection." ); return false; }
  3.  
to
Expand|Select|Wrap|Line Numbers
  1. if ((form.age.selectedIndex == 0 ) || (form.card.selectedIndex == 0))
  2. {
  3.     alert ( "Please make a selection." );
  4.     return false;
  5. }
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]
Jun 24 '08 #6
ammweb
6 New Member
Oh, I see the error in your previous code. Change this:
Expand|Select|Wrap|Line Numbers
  1. if ( form.age.selectedIndex == 0 ) || (form.card.selectedIndex == 0)
  2. { alert ( "Please make a selection." ); return false; }
  3.  
to
Expand|Select|Wrap|Line Numbers
  1. if ((form.age.selectedIndex == 0 ) || (form.card.selectedIndex == 0))
  2. {
  3.     alert ( "Please make a selection." );
  4.     return false;
  5. }
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?


Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. <!--
  3. function ValidateForm(form){
  4.       if ((form.age.selectedIndex == "" ) && (form.card.selectedIndex == 0))
  5.          {
  6.              alert ( "Please make a selection." );
  7.              return false;
  8.          }
  9.      else 
  10.          {
  11.          alert ("You have made your selection.");
  12.           return true;
  13.             }
  14.         }
  15. -->
  16. </script>
Thanks again for all your help. I have learned a lot with this..
Jun 24 '08 #7
acoder
16,027 Recognized Expert Moderator MVP
Of course, you're checking if all of them are not selected, so it would be &&, not ||
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.
Expand|Select|Wrap|Line Numbers
  1. var selects = form.getElementsByTagName("select");
  2. var anySelected = false;
  3. for (i = 0; i < selects.length; i++) {
  4.     if (selects[i].selectedIndex != 0) {
  5.         anySelected = true;
  6.         break;
  7.     }
  8. }
  9. if (!anySelected) {
  10.     // alert error
  11.     return false;
  12. }
Jun 24 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
2723
by: middletree | last post by:
Posted this to Access group, meant to do it here: I have what I call a composite table. Can't recall what they called it in database class, but it's where you take the PK of two different tables, and make a new table consisting of only those two fields. That's for normalization purposes, to resolve a many-to-many situation. Anyway, I...
1
894
by: mujeebrm | last post by:
/* menu.c, bc 5.02/tc 3, winxp */ #include <stdio.h> #include <conio.h> #define uaro 72 #define daro 80 /* plz help me in this logic, how can i modify this my program of a simple menu so that when i press down arrow key it
2
1625
by: kennygee | last post by:
I am trying to learn VB after many years programming in Access. I am trying to do something in VB that is simple in Access. How do I display record data on a form based on the selection made in a combo box? My problem may lie in not understanding which event is triggered when a new value is selected in the combo. I am working on the basis that...
2
1206
by: subbaraju | last post by:
I have two selection controls and based on the selection made by the user i need to pass those values to the other program in java...... can u give me the way......
2
3095
by: manishamca | last post by:
In dropdown i am retriving value from the backend. For eg: in the dropdown list the value selected are employee id..then i want to disply the emp name,job etc in the textbox. If i select a optino in dropdown menu then that id name etc shuld come in the textbox without refreshing the page... plz any help....... <%@ page...
8
1370
by: manishamca | last post by:
i want to retrive values from backend depending on the selection made in choice box. i have two choice box and different values shuld be retrived depending on the selection made in this two different box. is it possible to do this in jsp using javascript becz i am not familiar with ajax. Thanx in advance
5
1874
by: jmartmem | last post by:
Greetings, I am using Dreamweaver CS3 to design an ASP page that contains an Insert Record Form. Within this form are two list/menu form fields that I would like to "synchronize". In other words, let's say one list/menu is where the user selects "Program Name" and a second is for selecting "Project Name". If the user makes a selection from the...
2
1529
by: ash42 | last post by:
I need to write an asp application consisting of a webform which allows a user to make a selection from menu and display all jobs relalted to IT or all jobs related to Admin . A user will click on buttons 'IT' or 'Admin'. This will invoke a database to be read for the appropriate search - and then it should display a screen of all IT related...
0
7918
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.