Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple select boxes to be validated 1

Status
Not open for further replies.

hazelsisson

Programmer
Mar 18, 2002
68
GB
Hello!
I've got a page which contains multiple (dynamically created) select boxes, and I'm trying to check that at least one of them has a selected value before the form is submitted.
The select boxes all have unique names, and are all single selects.

Here's the code:
Code:
function validateForm()
{
var chosen = "n";
var form = document.qForm;
for (i = 0; i < form.elements.length; i++)
{
 if (form.elements[i].type == "select-one")
 {
  if (form.elements[i].options[form.elements[i].selectedIndex].value != "")
  {   
   chosen = "y";
  }
 }
}
if (chosen == "n")
{
 alert("Please choose a quote!")
}
else
{
document.qForm.submit();
}
}

The error (object doesn't support this property or method) is pointing to this line :
Code:
if (form.elements[i].options[form.elements[i].selectedIndex].value != "")

Can anyone spot anything wrong with it?!
Thanks!
Hazel
 

You are assuming that all select boxes have had a value chosen. Try changing this:

Code:
if (form.elements[i].options[form.elements[i].selectedIndex].value != "")

to this:

Code:
if (form.elements[i].selectedIndex > -1 && form.elements[i].options[form.elements[i].selectedIndex].value != "")

Hope this helps,
Dan

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top