473,791 Members | 2,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validation for multiple select list box

14 New Member
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 12285
RamananKalirajan
608 Contributor
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 Recognized Expert Moderator MVP
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 Top Contributor
@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
9920
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 validating a form that has a couple multi- select list boxes. The idea is that when the user submits the form, it goes to it's process script. The process script validates the responses, and if all is ok, finishes processing and "includes" a thank...
21
3925
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 adresses and so on. I thought it might be a better way to programm an automated, dynamic form validation that works for all kinds of fields, shows the necessary error messages and highlights the coresponding form fields.
4
4978
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 keep getting errors thrown that I can't verify because the form processes onto itself too quickly for me to check the Javascript errors) because the select multiple form element name has to be in the form of "var" because PHP will then recognize...
21
6258
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 full name is required. Please enter your full name!'); validity = false; frmComments.name.focus();
1
2638
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, this is a Select object with the "multiple" attribute. There are 20 options to choose from in this object; one of the options (we'll call it option X) should NEVER be selected with any other option in the object. Let's say that the user has...
5
2611
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 validation on both. Is there a way to link the drop-down choice to the correspondent validation section while disabling validation for the other one? here's what I presently have:
4
10847
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 selected and the total value is equal to 6, else they get an alert message. The validation is performed on an asp page. Would appreciate some help as I am lost with js!!
27
4759
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 appears that the data goes straight to the processing page, rather than the javascript seeing if data is missing and popping up an alert. I thought it may be because much of the form is populated with data from the db (lists, etc.), but when I leave...
11
3002
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 certain fields match certain criteria, and inform the user in different ways when the data is wrong (offcourse, this will be checked on posting the data again, but that's something I've got a lot of experience with). Now, offcourse it's...
0
9669
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
9515
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10154
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
9029
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
7537
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
5430
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4109
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

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.