473,664 Members | 2,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validating multiple select boxes

4 New Member
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="qtyItsall greek" id="qtyItsallgr eek">
<option value="0" selected="selec ted">0</option><option value="1">1</option>
<option value="2">2</option><option value="3">3</option>
</select>
<!-- select box 2 -->
<select name="qtyBluebe rrynights" id="qtyBlueberr ynights">
<option value="0" selected="selec ted">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.getEle mentById.elemen t.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 1578
acoder
16,027 Recognized Expert Moderator MVP
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
metafizzical
4 New Member
Thanks a lot. Seems obvious now. :-)
Aug 11 '08 #3
acoder
16,027 Recognized Expert Moderator MVP
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
8869
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
1943
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 want to change or add options to a drop down box, I then have to copy that to the other 11 boxes. Would it be possible to define the options once, and then have all of the drop down boxes get their options from the single list? Here is my...
3
2167
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 onchange/on blur to activate a javascript function to check the input for correctness once user moves out of the input box (tab or mouse click). If it looks OK, then nothing happens. But if an error is detected, the function puts out an alert box...
0
8861
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...
0
8778
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
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
8636
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6187
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
4185
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
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2764
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
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.