Click to See Complete Forum and Search --> : What is wrong with this for loop code? please help


K Lo
11-14-2003, 04:21 AM
Hi can someone tell me what is wrong with the following codes:

function ValidateAgainstList(field1){
ListA=document.forms[0].tmp.value
ListAArray=ListA.split("\n");
var max=ListAArray.length;
var z;

for (z=0; z<max ; z++){
if (field1==ListAArray[z]) {
alert("duplicate");
return true;
break;
}
}
}

the value of field1 is Apple, while ListAArray returns Apple, Orange, Pear;
HOWever, the above code never comes in to the alert statement!

However, if field1 value is Pear, then i will get the alert statement.

Any one see anythings wrong?

Charles
11-14-2003, 05:42 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>

<script type="text/javascript">
<!--
function validate (f) {
var listA = {apple : 1, orange : 1, pear : 1}
if (listA[f.fruit.value.toLowerCase()]) {alert ('duplicate'); f.fruit.value = ''; return false}
}
// -->
</script>

<form action="" onsubmit="return validate(this)">
<div>
<label>Fruit?<input type="text" id="fruit"></label>
<button type="submit">Submit</button>
</div>
</form>

And see the Java Naming Conventions (http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html).