473,761 Members | 9,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Atleast one selection must be made - DropdownList

107 New Member
I have four dropdownlist and i want to validate (client-side validation) that atleast one is selected before the form is valid for submission. I have tried the method below but it seems not working.
Expand|Select|Wrap|Line Numbers
  1.  
  2. <script language = "Javascript">
  3. var filter = new Array (3);
  4. filter[0] = "filter1"  //first dropdownlist name
  5. filter[1] = "filter2"
  6. filter[2] = "filter3"
  7. filter[3] = "filter4"
  8.  
  9.  
  10. function validate(){
  11.    for (var i = 0; i < filter.length; i++){
  12.       if (document.getElementById(filter[i]).value != '') {
  13.          return true;
  14.       }
  15.    }
  16.    alert("Please make atleast one selection");
  17.    return false;
  18. }
  19. </script>
And i have called the validate function as below:

<form method="POST" name="form" id="form" onsubmit='retur n validate()'>

Can somebody help me?
Aug 22 '12 #1
14 2676
gits
5,390 Recognized Expert Moderator Expert
how does a selectbox in your page look like? (HTML)
Aug 22 '12 #2
Rabbit
12,516 Recognized Expert Moderator MVP
The function works perfectly fine for me with the mock up HTML I made. Most likely your HTML for your form is wrong.
Expand|Select|Wrap|Line Numbers
  1. <script language = "Javascript"> 
  2. var filter = new Array (3); 
  3. filter[0] = "filter1"  //first dropdownlist name 
  4. filter[1] = "filter2" 
  5. filter[2] = "filter3" 
  6. filter[3] = "filter4" 
  7.  
  8.  
  9. function validate(){ 
  10.    for (var i = 0; i < filter.length; i++){ 
  11.       if (document.getElementById(filter[i]).value != '') { 
  12.          return true; 
  13.       } 
  14.    } 
  15.    alert("Please make atleast one selection"); 
  16.    return false; 
  17. </script> 
  18.  
  19. <body>
  20.     <form method="POST" name="form" id="form" onsubmit='return validate()'>
  21.         <select id="filter1">
  22.           <option value="">Choose</option>
  23.           <option value="saab">Saab</option>
  24.         </select>
  25.         <select id="filter2">
  26.           <option value="">Choose</option>
  27.           <option value="mercedes">Mercedes</option>
  28.         </select>
  29.         <select id="filter3">
  30.           <option value="">Choose</option>
  31.           <option value="audi">Audi</option>
  32.         </select>
  33.         <select id="filter4">
  34.           <option value="">Choose</option>
  35.           <option value="saab">Aston Martin</option>
  36.         </select>
  37.         <input type="submit" value="submit" />
  38.     </form>
  39. </body>
Aug 22 '12 #3
raamay
107 New Member
thanks guys for your help, but i am surprised it doesn't work. My selectbox looks like below, its a joomla frontend page without having used the joomla validation.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. defined('_JEXEC') or die('Restricted access');
  3.  
  4. JHTML::_('behavior.calendar');
  5. JHtml::_('behavior.tooltip');
  6. JHTML::_('behavior.formvalidation');
  7. ?>
  8. <script language = "Javascript">
  9. var filter = new Array (3);
  10. filter[0] = "filter1"
  11. filter[1] = "filter1"
  12. filter[2] = "filter1"
  13. filter[3] = "filter1"
  14.  
  15.  
  16. function validate(){
  17.    for (var i = 0; i < filter.length; i++){
  18.       if (document.getElementById(filter[i]).value != '') {
  19.          return true;
  20.       }
  21.    }
  22.    alert("Please make your selection");
  23.    return false;
  24. }
  25. </script>
  26. <form method="POST" name="form" id="form" onsubmit='return validate()'>
  27.     <div align="center">
  28.         <table id="mytable">
  29.             <tr>
  30.                 <td>
  31.                 <div align="center">
  32.                 <table style="width:42%">
  33.                 <tr>
  34.                     <td colspan="2"><h2 align="center">Filter and Search</h2></td>
  35.                 </tr>
  36.                 <tr>
  37.                     <td>Class</td>
  38.                     <td>
  39.                         <select name="filter1" id="filter1" class="inputbox1" style="width: 140px">
  40.                             <option value="">Filter by Class</option>
  41.                             <?php echo JHtml::_('select.options', JFormFieldCClass::getOptions(), 'clid', 'cclass');?>
  42.                         </select>
  43.                     </td>
  44.                 </tr>
  45.                 <tr>
  46.                     <td>Category</td>
  47.                     <td>
  48.                         <select name="filter2" id="filter2" class="inputbox1" style="width: 140px">
  49.                             <option value="">Filter by Category</option>
  50.                             <?php echo JHtml::_('select.options', JFormFieldCategory::getOptions(), 'catid', 'category');?>
  51.                         </select>
  52.                     </td>
  53.                 </tr>
  54.                 <tr>
  55.                     <td>Client Category</td>
  56.                     <td>
  57.                         <select name="filter3" id="filter3" class="inputbox1" style="width: 140px">
  58.                             <option value="">Filter by Client</option>
  59.                             <?php echo JHtml::_('select.options', JFormFieldClsCategory::getOptions(), 'clsid', 'clscategory');?>
  60.                         </select>
  61.                     </td>
  62.                 </tr>
  63.                 <tr>
  64.                     <td>Paper</td>
  65.                     <td>
  66.                         <select name="filter4" id="filter4" class="inputbox1" style="width: 140px">
  67.                             <option value="">Filter by Paper</option>
  68.                             <?php echo JHtml::_('select.options', JFormFieldPaper::getOptions(), 'pid', 'paper');?>
  69.                         </select>
  70.                     </td>
  71.                 </tr>
  72.                 <tr>
  73.                     <td ></td>
  74.                     <td ></td>
  75.                 </tr>
  76.                 <tr>
  77.                     <td ></td>
  78.                     <td> 
  79.                         <button>Search</button>
  80.                     </td>
  81.                 </tr>            
  82.             </table>  
  83.             </div>
  84.             </td>
  85.             </tr>
  86.             </table>            
  87.     <input type="hidden" name="option" value="<?php echo JRequest::getVar('option');?>"/>
  88.     <?php echo JHTML::_( 'form.token' ); ?>    
  89.     </div>
  90. </form>
  91.  
Aug 23 '12 #4
Rabbit
12,516 Recognized Expert Moderator MVP
What does the HTML look like on the client side?
Aug 23 '12 #5
raamay
107 New Member
Expand|Select|Wrap|Line Numbers
  1. <form method="POST" name="form" id="form" onsubmit='return validate()'>
  2.     <div align="center">
  3.         <table id="mytable">
  4.             <tr>
  5.                 <td>
  6.                 <div align="center">
  7.                 <table style="width:42%">
  8.                 <tr>
  9.                     <td colspan="2"><h2 align="center">Search Tenders</h2></td>
  10.                 </tr>
  11.                 <tr>
  12.                     <td>Class</td>
  13.                     <td>
  14.                         <select name="filter1" id="filter1" class="inputbox1" style="width: 140px">
  15.                             <option value="">Filter by Class</option>
  16.                             <option value="26">L</option>
  17. <option value="27">M</option>
  18. <option value="28">S</option>
  19.                         </select>
  20.                     </td>
  21.                 </tr>
  22.                 <tr>
  23.                     <td>Category</td>
  24.                     <td>
  25.                         <select name="filter2" id="filter2" class="inputbox1" style="width: 140px">
  26.                             <option value="">Filter by Category</option>
  27.                             <option value="22">W1</option>
  28. <option value="23">W2</option>
  29. <option value="24">W3</option>
  30. <option value="25">W4</option>
  31.                         </select>
  32.                     </td>
  33.                 </tr>
  34.                 <tr>
  35.                     <td>Client</td>
  36.                     <td>
  37.                         <select name="filter3" id="filter3" class="inputbox1" style="width: 140px">
  38.                             <option value="">Filter by Client </option>
  39.                             <option value="13">Government</option>
  40. <option value="12">Private</option>
  41.                         </select>
  42.                     </td>
  43.                 </tr>
  44.                 <tr>
  45.                     <td>Paper</td>
  46.                     <td>
  47.                         <select name="filter4" id="filter4" class="inputbox1" style="width: 140px">
  48.                             <option value="">Filter by Paper</option>
  49.                             <option value="11">Times</option>
  50. <option value="10">Business</option>
  51.                         </select>
  52.                     </td>
  53.                 </tr>
  54.                 <tr>
  55.                     <td ></td>
  56.                     <td ></td>
  57.                 </tr>
  58.                 <tr>
  59.                     <td ></td>
  60.                     <td> 
  61.                         <button>Search</button>
  62.                     </td>
  63.                 </tr>            
  64.             </table>  
  65.             </div>
  66.             </td>
  67.             </tr>
  68.             </table>            
  69.         </fieldset>
  70.     <input type="hidden" name="option" value="com_filters"/>
  71.     <input type="hidden" name="412f9f21029443c16fad2f9ddbda9369" value="1" />    
  72.     </div>
  73. </form>
Aug 23 '12 #6
raamay
107 New Member
does anybody have any idea? i too tried the function as suggested by RABBIT and it works in a mock html page but when i implement it in my actual form it doesn't.
Aug 23 '12 #7
Rabbit
12,516 Recognized Expert Moderator MVP
It's because your button isn't a submit button.
Expand|Select|Wrap|Line Numbers
  1. <button type="submit">Search</button>
Aug 23 '12 #8
raamay
107 New Member
i tried your solution rabbit but still the result is out of question. But please note that the validation works in IE7 and only in firefox and chrome it has no effect.

does it have anything to do with mootools in joomla? but infact i ve not used the joomla validation
Aug 24 '12 #9
Rabbit
12,516 Recognized Expert Moderator MVP
It would help had you said that at the beginning. I don't have chrome or firefox at work.
Aug 24 '12 #10

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

Similar topics

7
2732
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 have three dropdowns, which I intentionally gave the same name. I want people to select one, two, or...
1
1319
by: Michael Groeger | last post by:
Hi, I want to be able to have a DropDownList which supports multiple selection, but I haven't found a property for that in the DropDownList control. Is there a way to have multiple selection with that one. Kind Regards, Michael
4
1612
by: Nick Zdunic | last post by:
Is there an example of using client side script to disable selection of some items in a drop down that I could follow. It would seem tricky as there are multiple drop downs in the GridView. I would need to remember the last selected value for each drop down within the grid. If the selected value is not permitted then reset to the remembered value. And then when a valid item is selected the remebered value for that drop down would...
2
6184
by: WT | last post by:
Hello, I can figure how to set selectIndex or Value, I have tried this <EditItemTemplate> <asp:DropdownList ID="DDLSector3" DataSource='<%# BindSector(this,(int)DataBinder.Eval(Container.DataItem,"wt_SectorId"))%>' OnPreRender='SetSectorIndex' DataTextField='wt_SectorName' DataValueField='wt_SectorId' runat="server" >
2
1631
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 when the new value is selected, I can then use the new value to reposition the data in the forms...
2
1221
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......
8
1382
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
7
5542
by: ammweb | last post by:
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> <head> </head> <body> <table width="100%" height="500px" border="0" cellpadding="0" cellspacing="0">
5
3004
Claus Mygind
by: Claus Mygind | last post by:
I have a list box and want to limit the user to selecting a max of 5 items from the list. I've put in a counter which warns the user that more than 5 items have been selected, however I cannot reverse the users selection of the last item in the select box as I cannot identify which of the items was the last. I store the list of selected items in a hidden field on change. That one is no problem, but I want to reflect which items have been...
2
1538
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 jobs or Admin related jobs .5 jobs per page must be shown before pressing 'More' for next screen -...
0
9554
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9988
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9923
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8813
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3911
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
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.