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

Counting the number of input fields on a page 1

Status
Not open for further replies.

Stoemp

Programmer
Sep 25, 2002
389
BE
Hi,

I'm trying to develop a script to validate a form's input. The difficulty is that I want to make the script work for different pages. The content of the pages changes. If I make a regular validation I have to make a new one for every page because the fields change. So here's what I want to do:

I want to access a kind of array of input fields on the page
something like:
for (x = 1 to document.form.input.count){
if (document.form.input.name.value == "") then blabla
}

Is this possible in any way?

Thanks,
Steven
 
Hi

Yes, something like this:

for (x=0; x < document.FormName.elements.length; x++){
if (document.FormName.elements[x].value == &quot;&quot;){
//some code }
}

To be helpful to the user though, the //some code should give an indication of which field is empty - you can use the document.FormName.elements[x].name value for that.

Hope that helps
:)
 
Indeed this helped! Thank you very much! The script is amazing now!
 
One addition:

f=document.FormName; //just to make it shorter

for (x=0; x < f.elements.length; x++){
if (f.elements[x].type == &quot;text&quot; && f.elements[x].value == &quot;&quot;)
//do something
}

This will make sure that you deal with the desired element types only.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top