Avatar of saabStory
saabStoryFlag for United States of America

asked on 

Need some help with a javascript validation loop

My apologies - I'm sure the answer it dead simple here but I've got a tight deadline and am having trouble with this validation and need some help.

Part of my form allows a person to add up to 10 volunteers and fill out their information too.  The additional volunteers are added one at a time.  The fields are named ldrFName1, ldrLName1, ldrFName2, ldrLName2, etc up to 10 (there's also address, city, zip, phone and all but I can extrapolate the rest).

I know I can loop through the fields and save a ton of hand coding but, so far, my feeble attempts aren't working for me.  I know I have to be building the dynamic field name wrong and although it is alerting correctly, the validation isn't stopping - so I know the field names are being recognized or I've got an error that I don't see.

I'm getting the count for the loop from a hidden field that is updated as additional volunteer fields are added or subtracted by the buttons that add a new volunteer or cancel one.

If I were just writing out the validation for a single field, it would look something like the attached code:

So if I have to loop it, how do I properly write the field names within the loop so they're picked up by the validation?

BTW, I'm not including any of the supporting functions for the validation but can if needed.

Thanks a lot in advance.

function validateForm(theForm){
	strMessage ='Please correct the following problem(s)!\n\n';
	var iErrorCount = 0;
	var f = document.theForm;


/*= VALIDATE LEADER FIRST NAME =*/
for (var i=0; i<f.ldrCounter.value; ++1) {
	if(notEmpty(f.ldrFName1.value)==false){
		strMessage = strMessage +"The first name of the leader is required.\n";
		if(iErrorCount ==0){
			f.ldrFName1.focus();
			f.ldrFName1.style.background = errorColor;
		}
		iErrorCount ++;
	} else {
		f.ldrFName1.style.background = backColor;
	}
}

	if (iErrorCount > 0){
		alert(strMessage);
		strMessage ='';
		iErrorCount =0;
		return false;
	}else{
	}
}

Open in new window

JavaScript

Avatar of undefined
Last Comment
saabStory
ASKER CERTIFIED SOLUTION
Avatar of Sudhindra A N
Sudhindra A N
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of saabStory
saabStory
Flag of United States of America image

ASKER

Works perfect - just what I needed.  Now i can finish this thing and have some weekend left!

Many thanks.
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo