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

Validating multiple select boxes

Hi,

I'm currently trying to write a loop that can be used on several select boxes within the same form.

Some sample HTML:
[HTML]<!-- select box 1 -->
<select name="qtyItsallgreek" id="qtyItsallgreek">
<option value="0" selected="selected">0</option><option value="1">1</option>
<option value="2">2</option><option value="3">3</option>
</select>
<!-- select box 2 -->
<select name="qtyBlueberrynights" id="qtyBlueberrynights">
<option value="0" selected="selected">0</option><option value="1">1</option>
<option value="2">2</option><option value="3">3</option>
</select>[/HTML]

All form validation is done once the user hits a button that calls my main() function, which in turn calls a check_input function:

Expand|Select|Wrap|Line Numbers
  1. function check_input(element) {
  2.     if (element.match(/^qty.*$/)) {
  3.      var x = document.getElementById('orderform').qtyItsallgreek.selectedIndex;
  4.      var y = document.getElementById.element.selectedIndex;
  5.      return true;
  6.     }
  7. }
  8. function main() {
  9.     var myForm = document.getElementById('orderform');
  10.         for (var i = 0; i < myForm.elements.length; i += 1) {
  11.             var e = myForm.elements[i];
  12.             if (e.type === "select-one") {
  13.                 check_input(e.id);
  14.             }
  15. }
  16.  
While var x contains the selectedIndex value, var y is interpreted literally, which gives the error that document.getElementById.element.selectedIndex is undefined.

Instead of referring to each specific select box like var x, is it possible to use the element variable in a similar way to var y?

You might have noticed I'm quite new to JavaScript :-)

PS. In the second code block on line 3, there shouldn't be a space within the word 'greek' - It's a non-breaking space that I can't get the message editor to remove :-)
Aug 10 '08 #1
3 1568
acoder
16,027 Expert Mod 8TB
Since you're passing the ID, just pass the value to the getElementById method:
Expand|Select|Wrap|Line Numbers
  1. var y = document.getElementById(element).selectedIndex;
  2.  
Aug 11 '08 #2
Thanks a lot. Seems obvious now. :-)
Aug 11 '08 #3
acoder
16,027 Expert Mod 8TB
You're welcome. Post again back to the forum if you have more questions :)
Aug 12 '08 #4

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

Similar topics

12
by: Forti2ude | last post by:
Hello, I have a simple form... <form> <select name="foo" multiple> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select>
4
by: teknoshock | last post by:
I have created a page with multiple drop down boxes, all populated with the same options. My problem is, for 12 dropdown boxes and 40 choices per box, I end up with a massive file. Also, if I...
3
by: Louis | last post by:
I have a form with multiple input boxes. I want to validate each input box (and force user to correct it) before allowing user to move to another, either using tab key or a mouse click. I try...
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...
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
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,...
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.