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