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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation dynamic number of select lists

Status
Not open for further replies.

mrmookster

Programmer
Feb 8, 2005
27
GB
i've reached the limit of my javascript knowledge can you help?

i have a form that builds a number of select boxes (it may be different per page) i need a javascript form validator to go through each select in the form and verify that a selection has been made ie selectedIndex !=0

the pseudocode would be

function validateForm
errorStr = ""
for each select in form
if select.selectedIndex != 0 then
errorStr = errorStr + select.selectedIndex.text
next

if errorStr <> "" then alert(errorStr)
return false
else return true
end function
 
Hey, I think the first thing you need to do is get all the select elements. something like
Code:
// get all select elements on the page
var selectArray = document.getElementsByTagName('select');

Then once you have an array of all the select elements its a matter of looping through each one and checking its current state.
Code:
for (var i = 0; i < selectArray.length; i++) {
if (selectArray[i]...) your code here
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top