Solved

Javascript Validation for Dropdown list array

Posted on 2004-07-30
6
294 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How search webpage "viewable text" only 1 75
razorCMS: Change Menu Font 4 59
modify h2 4 114
Two wordpress questions 3 43
Read about why website design really matters in today's demanding market.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to count occurrences of each item in an array.

813 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

15 Experts available now in Live!

Get 1:1 Help Now