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

loop through 20 variables

Status
Not open for further replies.

omoo

Programmer
May 30, 2005
87
0
0
US
Hi, I have the following code to validate the fields of a form. I have 20 of such field (filesize1, filesize2,...,filesize20). Can anyone help to advise me on what to add to this code so that it can loop through all the 20 variables? Bascially, I need to change the filesize variable so that it can change to filesize1, ... , filesize20 in the loop.

Code:
		if (txtselfilesize.value!="")
		{
			if (isNumericOrDecimal(txtselfilesize)==false) 
			{
            			mesg = "You have entered <" + txtselfilesize.value + "> for Filesize\n"
				mesg = mesg + "Please enter a numerical value."
            			alert(mesg)
            			txtselfilesize.focus();
   				return false;
        	}
        }
 
I've not tested this, but give it a whirl:

Code:
for (var loop=1; loop<21; loop++) {
	var field = document.forms['YourFormNameHere'].elements['filesize' + loop];

	if (field.value != '') {
		if (!isNumericOrDecimal(field)) {
			mesg = 'You have entered <' + txtselfilesize.value + '> for Filesize' + loop + '\n';
			mesg = mesg + 'Please enter a numerical value.';
			alert(mesg);
			field.focus();
			return (false);
		}
	}
}

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top