/    Sign up×
Community /Pin to ProfileBookmark

I have a form validation script that will not exit a loop. I initially loop through all the elements in the form. for each element I am running a second loop to test if the element value is in my array of excluded form fields. If one is found it increments the first loops counter – iCounter. Then a second IF statement checks to see if my exclusion list holds any sequential numbers. If not it breaks from the second loop and continues with the validation of form.element[iCounter]. The problem is my two alert boxes in the code show up with their inital values repeatedly although I’ve incremented the first counter already.

function Validate()
{
var form = document.frmLeapApp
var iCounter;
var returnValue = true
var formControl;
var i;
var aExcludeFields = new Array(15,17,23,26,27,35,36,37,38);

//Loop through the form fields
for (iCounter = 0; iCounter < form.length; iCounter++)
{
//Check for form elements not required to be validated
// in the following statement
for (i = 0; i < aExcludeFields.length; i++)
{
if (iCounter = aExcludeFields[i])
{
alert(“Found element ” + iCounter); //for debugging ex. 15
iCounter++;
alert(iCounter + ” ” + aExcludeFields[i + 1]); //for debugging ex 16 17

//now evaluate the next array value to see if we have any numbers in sequence. IF 16 != 17 break from the loop
if (parseInt(iCounter) !== parseInt(aExcludeFields[i + 1]))
{
//we can finally exit the FOR loop and continue validation
break;
}// end IF
}//end IF
} //end for loop

//get the form element and value then test it for various conditions
formControl = form.elements[iCounter];

if (formControl.value == “”)
{
alert(“Please specify a value for ” + formControl.name + ” field. “);
returnValue = false;
return returnValue;
formControl.focus();
break;
} //end IF statement

}//end for loop

}//end function

The logic to me seems clear cut. Any idea whats wrong?

Ken

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@PittimannJun 04.2004 — Hi!

if (iCounter =[COLOR=blue]=[/COLOR] aExcludeFields[i])



At first sight, you are missing the blue "=" above.



Cheers - Pit
×

Success!

Help @beruken spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...