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!

Checking dynamically named forms

Status
Not open for further replies.

cathl

Technical User
Aug 4, 2000
18
0
0
GB
Hi, I am trying to use Javascript to check a form which is named dynamically but my code isn't working. My cgi script creates a HTML page which contains forms named form0, form1, form2 etc. Each form contains a field named type_id, a field named budget and a field named start_date.
Here is my Javascript function:
function nextCheck(tot)
{
var missinginfo = "";
if (document.forms[tot].type_id.value=="")
{
missinginfo += "\n Budget Type";
}
if (document.forms[tot].budget.value=="")
{
missinginfo += "\n Budget";
}
if (document.forms[tot].start_date.value=="")
{
missinginfo += "\n Start Date";
}
if (missinginfo != "")
{
missinginfo = "Please fill in the following:\n"+missinginfo;
alert(missinginfo);
return false;
}
else
{
return true;
}
}

I am calling the function from the submit button like this:
onClick="return nextCheck(form$i);"
(where $i is a number generated by the cgi script)
Can anyone tell me where I'm going wrong? Thanks.
 
form1 or form3 or whatever isn't enough for the browser to find the element
either build the var in the function : eval("document."+tot)
or pass the actual form object <form ... onsubmit=&quot;return nextCheck(this)&quot;> (or if you want to keep calling it form the button return nextCheck(this.form)) ------
please review FAQ183-874 - this will help you to get the best out of tt
[ &quot;you&quot; is not someone in particular - don't take it too personnal ]
 
does it work ? which solution work ? i'm asking this, for people who come & have the same problem as you do - so they'll have question + answer in the same thread and don't have to reask again ------
please review FAQ183-874 - this will help you to get the best out of tt
[ &quot;you&quot; is not someone in particular - don't take it too personnal ]
 
OK here is the solution I used:
function nextCheck(obj)
{
var missinginfo = &quot;&quot;;
if (obj.type_id.value==&quot;&quot;)
{
missinginfo += &quot;\n Budget Type&quot;;
}
if (obj.budget.value==&quot;&quot;)
{
missinginfo += &quot;\n Budget&quot;;
}
if (obj.start_date.value==&quot;&quot;)
{
missinginfo += &quot;\n Start Date&quot;;
}
if (missinginfo != &quot;&quot;)
{
missinginfo = &quot;Please fill in the following:\n&quot;+missinginfo;
alert(missinginfo);
return false;
}
else
{
return true;
}
}

and I called the function from the submit button using:
onClick=&quot;return nextCheck(this.form)&quot;
Thanks again!
 
thank YOU for posting the complete answer :)
that's nice and i hope it will help other people as well :) ------
please review FAQ183-874 - this will help you to get the best out of tt
[ &quot;you&quot; is not someone in particular - don't take it too personnal ]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top