Click to See Complete Forum and Search --> : checking form's fields


yaron
09-16-2003, 03:35 AM
Hello all (first time here),
I have a form with an unknown number of fields.
the name of each filed is: fixed name + number
e.g. field1,field2,field3.....field189....
When a submit commant is taking place I call to a javascript function that needs to go through all of these fields.
I use a loop and on each iteration of the loop I change the filed name like this:
for(i=0;i<num;i++)
{
ifiled=field+i;
if(form.ifield.value='')
//do something
}

the field variable is sent to the javascript function.
When I write form.ifield.value I get the error that it is not an object though the variable ifield contains the correct field name!!
How can I use variables to go through my fields?
I hope my problem is understood
Thanks

yaron
09-16-2003, 03:48 AM
the variable is treated as the field's name where in fact the variable's value is the field's name!!
Is there a way around it?

Superfly1611
09-16-2003, 04:11 AM
ok i presume you are dynamically creating an unknown numbe rof fields and hence cant give it a fixed value in the loop?

What you can do is get teh javascript to check to see if a field actually exists before checking it.

This is simply achieved by

if(document.all.fieldname)
{
//your function eg
if(document.all.fieldname.value.length > 0)
{
alert("Greetings " + document.all.fieldname.value);
}
}
else
{

return false;
}

Also make sure that
i'm also not sure if you can concatonate a object name like that - i've had random success like that before.
The only way i know that is fool proof is....

variable = document.getElementById("field" + i);

making sure that the html element ID property is set accordingly.

Hope that helps
My Javascript isn't my strong point but this is one thing that i have to do on an all to frequent basis as i'm forever dynamicaly creating forms in ASP.

Webskater
09-16-2003, 05:09 AM
Javascript is not my strong point either but isn't this what eval is for?

I think this:
ifiled=field+i;
if(form.ifield.value='')

Should be:
ifiled=eval('field'+i);
if(form.ifield.value='')

but, then again, I find this does not work consistently!