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

Looping on repeated fields

Status
Not open for further replies.

Microbe

Programmer
Oct 16, 2000
607
AU
I have some code that looks like this

====================
if (theForm.location1.value < 1000 || theForm.location1.value > 9999){
alert(&quot;Invalid postcode. A valid postcode has 4 numbers&quot;)
theForm.location1.select()
return(false);
}
if (theForm.location2.value < 1000 || theForm.location2.value > 9999){
alert(&quot;Invalid postcode. A valid postcode has 4 numbers&quot;)
theForm.location2.select()
return(false);

and so on
=====================

What I want to do is make section of code to tidy it up so that it looks something like this...but it looks very wrong...any thoughts?

=======================
for(count = 1; count < 10;count++){

if (&quot;theForm.location&quot; + count + &quot;.value < 1000 || theForm.location&quot; + count + &quot;.value > 9999&quot;){
alert(&quot;Invalid postcode. A valid postcode has 4 numbers&quot;)

//this is the bit where I am stuck...what would this line look like?
//theForm.location + count + .select() looks totally wrong.

theForm.location1.select()
return(false);
}
}
==============

In VBscript it is a breeze, but I am a bit stuck here. Perhaps it can't be done...

Steve Davis
hey.you@hahaha.com.au
 
Microbe,

Try this:

for(count = 1; count < 10;count++){

if (eval(&quot;theForm.location&quot; + count).value < 1000 || eval(&quot;theForm.location&quot; + count).value > 9999&quot;){
alert(&quot;Invalid postcode. A valid postcode has 4 numbers&quot;);
eval(&quot;theForm.location&quot; + count).focus();
eval(&quot;theForm.location&quot; + count).select();
return(false);
}
}
Mise Le Meas,

Mighty :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top