473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Atleast one selection must be made - DropdownList

107 100+
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='return validate()'>

Can somebody help me?
Aug 22 '12 #1

✓ answered by raamay

the above javascript code is working perfectly now. This is to inform all that if you are using joomla and if you don't need mootools validation then please disable it so that your custom javascript validation like above works. I disabled the use of mootools and now its working well.

14 2654
gits
5,390 Expert Mod 4TB
how does a selectbox in your page look like? (HTML)
Aug 22 '12 #2
Rabbit
12,516 Expert Mod 8TB
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 100+
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 Expert Mod 8TB
What does the HTML look like on the client side?
Aug 23 '12 #5
raamay
107 100+
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 100+
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 Expert Mod 8TB
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 100+
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 Expert Mod 8TB
It would help had you said that at the beginning. I don't have chrome or firefox at work.
Aug 24 '12 #10
ariful alam
185 100+
The code is working fine in ie6, firefox 10.0.2, opera 12.01, safari 5.1.2 and chrome 21.0.
Aug 25 '12 #11
raamay
107 100+
it works in IE7 too but still not working in FF 9 or 14 and Chrome. i am sure it has something to do with joomla only. Since this forum is not for joomla, so i don't understand how to solve this problem.
Aug 27 '12 #12
raamay
107 100+
the above javascript code is working perfectly now. This is to inform all that if you are using joomla and if you don't need mootools validation then please disable it so that your custom javascript validation like above works. I disabled the use of mootools and now its working well.
Aug 28 '12 #13
gits
5,390 Expert Mod 4TB
either that or make use of the Joomla built in tools like this: http://docs.joomla.org/Client-side_form_validation
Aug 28 '12 #14
raamay
107 100+
ya Expert gits, you are right but you may be aware Joomla has limited resources to refer to. It would be easy for us to use the built in joomla validation but i didn't know how to create custom validation like above using the built in support.
Aug 29 '12 #15

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

Similar topics

7
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...
1
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...
4
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...
2
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='<%#...
2
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...
2
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
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...
7
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...
5
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.