473,386 Members | 1,924 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,386 software developers and data experts.

Validation for multiple select list box

Hi,
I am using a multiple select list box.

I use an array to retrieve the values that are selected by the user.

Expand|Select|Wrap|Line Numbers
  1. <select name="competitor[]">
  2.  
When I try to validate the list box if the user does not select anything, I get errors. The error deals with the array declared.

I am using PHP and the validation is done in Javascript. Please help out.

Thanks in advance.
Apr 14 '09 #1
3 12214
RamananKalirajan
608 512MB
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function selectMultiple()
  5.   {
  6.     var myArray="";
  7.          try
  8.           {
  9.            for (var i=0; i<document.getElementById('mySelect').options.length; i++)
  10.           { 
  11.                       if (document.getElementById('mySelect').options[i].selected == true) 
  12.                        { 
  13.                           //alert(document.getElementById('mySelect').options[i].text);
  14.               myArray += document.getElementById('mySelect').options[i].text + ";";
  15.                         } 
  16.           }
  17.  
  18.            var resArray = myArray.split(";");
  19.            if(resArray.length<=1) 
  20.              alert("Please select the values in the list");
  21.            else 
  22.              alert("You have selected = "+(resArray.length-1));             
  23.          }
  24.         catch(e)
  25.         {
  26.            alert(e.message);    
  27.         }
  28.  
  29. }
  30. </script>
  31. </head>
  32. <body><form>
  33. <select name="mySelect" size="10" multiple>
  34.   <option>Apple</option>
  35.   <option>Pear</option>
  36.   <option>Pea</option>
  37.   <option>Banana</option>
  38.   <option>Orange</option>
  39.   <option>Mango</option>
  40.   <option>Lime</option>
  41.   <option>Goa</option>
  42.   <option>Dates</option>
  43. </select>
  44. <input type="button" onclick="selectMultiple()" value="Select multiple">
  45. </form></body>
  46. </html> 
This is a validation for the multiple select box. As you were specifying array. I did this way. But i am not clear wether r u looking for this kind of validation or not.

Regards
Ramanan Kalirajan
Apr 14 '09 #2
acoder
16,027 Expert Mod 8TB
I don't think that's what he meant and anyway, the code is incorrect because the select element has no ID.

You can get the element by name:
Expand|Select|Wrap|Line Numbers
  1. document.forms[formname].elements["competitor[]"];
then get its options and validate.
Apr 14 '09 #3
dmjpro
2,476 2GB
@Acoder
is "name[]" that a valid name?
Apr 15 '09 #4

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

Similar topics

1
by: Greg Bryant | last post by:
Hi folks. I'm porting a cf site to php, everything's going very well, I like php much better (this, of course, being the correct forum to make that statement :). One problem I have is with...
21
by: Stefan Richter | last post by:
Hi, after coding for days on stupid form validations - Like: strings (min / max length), numbers(min / max value), money(min / max value), postcodes(min / max value), telefon numbers, email...
4
by: Phil Powell | last post by:
Has anyone here ever done a case where you have a select multiple form element and you have to do both server-side and client-side validation? I am honestly not sure how to do it in Javascript (I...
21
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
1
by: Griff | last post by:
Hi! I'm using JavaScript on an HTML page for form validation. I have a form value (theForm.ProdType1) which represents a picklist from which users may make multiple selections; essentially,...
5
by: EviL KerneL | last post by:
Hi - I am trying to figure out a way to enforce the validation included for this form based on whether the user chooses "email" or "phone" as the contact choice. Right now it is set to enforce...
4
by: adam.lock | last post by:
Hi I have a form which has 10 list boxes on it, each with a select option with values 0 to 3. Once the user submits the form, I want to validate that only three of the list boxes have been...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.