Solved

Javascript Validation for Dropdown list array

Posted on 2004-07-30
6
285 Views
Last Modified: 2008-02-20
Hi,

I have used a select box array to capture multiple selected values as :

<select name="food[]" index="food" multiple>
  <option>Ham</option>
  <option>Coke</option>
  <option>Pizza</option>
  <option>Cheese</option>
</select>

This code works fine with PHP, but fails with javascript validation    :(

Any idea how do I do it...

Thanks,
0
Comment
Question by:guifreak
  • 2
6 Comments
 

Expert Comment

by:kevinKohler
ID: 11677075
Hello,

This is what you need to validation that data.

<script language="JavaScript">
function validatefood() {
Food=theForm.food.selectedIndex;
if (Food=='-1') {
alert('Please Select food');
event.returnValue=false;
}
}
</script>

<form name="theForm">
<select name="food" index="food" multiple>
  <option>Ham</option>
  <option>Coke</option>
  <option>Pizza</option>
  <option>Cheese</option>
</select>
<INPUT type="Submit" value="Submit Change" onclick="validatefood();">
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 11678393
You cannot have a select box name be an array.

If you want it in array format then have javascript assemble the array for you although really if you just have a regular variable it will submit as a delimited list and then you can turn it into an array in PHP
0
 
LVL 18

Accepted Solution

by:
arantius earned 100 total points
ID: 11689185
Assign an ID to your select tag:
<select name="food[]" index="food" multiple id="foodSelect">

And use the ID to refer to the element in javascript:
var sel=document.getElementById("foodSelect")
if (sel.selectedIndex ......
0
 
LVL 18

Expert Comment

by:arantius
ID: 11689189
BTW mrichmon: Yes you can have the name be an array.  That's how you tell PHP to take ALL selected values of that element, not just the last one.  And kevin, that's why he needs to keep it that way.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now