An if statement in a for loop problem

G’day I’m new in the javascript world and this code I have works but it’s not working the way I want it to. Was wondering if someone could take a look at it and point out what I’m doing wrong.

I have multi-valued fields which I’ve split into an array. In the IF statement, if it hits one of these values in the array that is equal to the value in the ‘DateEntered’ field then I want it to tell me in the alert message. The problem is, is that it gets to the first value in the array and the alert works but it stops there.

For instance. In the array are 4 dates. 02/22/02007, 02/23/2007, 02/19/2007 and 02/16/2007. If the date in the ‘DateEntered’ field is 02/22/2007 and I clik away from the field (onblur) then the alert message tells me ‘02/22/2007’. If I pick any other date in the array, it doesn’t do anything. It only works on the first one. I think my FOR LOOP with my IF statement is not right. Here’s the code:

Thanks for any help

Kenny

Subject: an if statement in a for loop problem

take out the return statement in the if. return does just that… it returns to the calling code the value that you specify (true, in this case) and stops execution at that point. In your example above, you state that it only alerts one time. In this example, only one date is equal to your target date, and there should only be one alert. However, if you want to show a message for a set of dates that might have a duplicate target date, modify the code as below.

function DateCheck(){

var DateEntered=document.forms[0].TableDate_1.value;

var ExistingDates=document.forms[0].ExistsHere.value;

var Quantities = document.forms[0].Quantities.value;

var Parts = document.forms[0].Parts.value;

arraydates = ExistingDates.split(“;”);

document.forms[0].ProdConflicts.value = “”;

for (var i=0; i < arraydates.length; i++){

if (arraydates[i] == DateEntered){

alert(arraydates[i])

//just a note here… I would add this to a string array instead of alerting for each one.

//if what you want is to set a value here to check later, then just set the variable. Having the return statement here is

//what was causing the incorrect action.

} //end if

} //end for

//and then here, i would use an alert (or write it to a message div on the form) to display all of the errors at once.

//this is much less intrusive

return false; //the could just be return; or it could be the value of the string that was good if you need to return that to the calling program

//return true and return false are both returns, with true/false being the boolean value returned

}

Subject: RE: an if statement in a for loop problem

Thanks Joel. That helps me out alot for my coding. About 5 min ago we just found out why it was only showing the one value in the array. It appears that the split method of the ; wasn’t split(“;”) but instead split("; "). The view column was putting a space after the semi-colons like 2/22/2007; 2/20/2007; 2/19/2007 etc.I did use your helpful tips in tidying up my code snippet.

Thanks again for your help

Kenny