Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ARRAYS

Status
Not open for further replies.

3li773

Programmer
Jan 23, 2003
20
0
0
CA
ok i've just started to use javascript
and i wanted to know if the user inputs something how can i check that to an array
 
say they enter "test"
save to a var name checkValue
var checkValue = document.myForm.txt.value;

your array would be something like
var myArray = new Array("something","somethingelse","somethingnew","test");

then loop through the array
for(var counter= 0; counter <= myArray.length; counter++){
if(checkValue == myArray[counter]) {
alert(&quot;evalue is eqaul to element &quot;+counter+&quot; in the array&quot;);
}
} _______________________________________________
[sub]{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }[/sub]
_______________________________________________
for the best results to your questions: FAQ333-2924
has you're questio
 
MAN YOU ARE MY HERO


THANX A BUNGE
 
ok say i want to do it so if they dont enter data that is in the array they get sent to a different page altogether

how would that happen
 
you can do some modifications to the above to redirect the viewer to say page in the site.
so say you have a list of pages. test is one of them
have the text box and a label saying enter page you would like to visit.
in the body
<form name=&quot;myForm&quot;>
<input type=&quot;text&quot; name=&quot;txtURL&quot; onBlur=&quot;goURL(this.value)&quot;>
</form>

then your function would look like this
<script>
var myArray = new Array(&quot;something.htm&quot;,&quot;somethingelse.htm&quot;,&quot;somethingnew.htm&quot;,&quot;test.htm&quot;);

function goURL(url) {
for(var counter= 0; counter <= myArray.length; counter++){
if(myArray[counter].indexOf(url)!==-1) {
alert(&quot;match&quot;);
document.location.href = myArray[counter];
break;
}
else {
alert(&quot;no match yet!&quot;);
}
}
}
</script>

notice two new things here.
indexOf()
what we do here is check to see if the value in the array that is in reference a this point of the counter has the value of what was entered intot he tet box at all. note:if you enter something the first page is where you go. this was jsut a example and would not be good for pages with like names.

and
location.href
which is jsut a redirection function

O' and the break;
if you don't do a break in the loop you get a undefned error due to the loop still going. you need to stop the loop at the point of a match and this is how you would in javascript

also notice the declaration of the array outside the function. this makes it global and can be thus used in any function.

is this kind of what you had in mind? _______________________________________________
[sub]{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }[/sub]
_______________________________________________
for the best results to your questions: FAQ333-2924
has you're questio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top