...

View Full Version : onKeyPress JS alert



Uzair
12-15-2006, 11:40 AM
I want a form text field in which when I type OK, the browser window should automatically redirect to GOOGLE.COM & when I type some other thing it show me alert. I'm using onKeyPress but it only let me type "O"
any idea how to control all this???

david_kw
12-15-2006, 05:08 PM
Try with onchange instead. That means they've typed in whatever they wanted and with a text field I think it has also lost focus meaning the person was done typing.

david_kw

Mr J
12-15-2006, 07:49 PM
Give this a try


<script type="text/javascript">

function test(){
str=document.getElementById("t1").value
re=/\bok\b/gi
isTrue=re.test(str)

if(isTrue){
location="http://www.google.com"
}
else{
alert("Yoo Hoo")
}

}

</script>

<input id="t1" type="text" value="" onchange="test()">

david_kw
12-15-2006, 07:56 PM
Does 'b' have a special meaning in regular expressions for javascript?

re=/bokb/gi

I would have expected

re=/^ok$/i

or something like that.

david_kw

liorean
12-15-2006, 09:20 PM
Does 'b' have a special meaning in regular expressions for javascript?No. All alphanumerical ASCII characters are literals in JavaScript regex. \b has special meaning, on the other hand.

re=/bokb/gi

I would have expected

re=/^ok$/i

or something like that.Yeah, that's what I'd have expected too :)

Mr J
12-16-2006, 12:57 PM
re=/^ok$/i

is the one to use

I was thinking of the boundary word \b

I am still getting to grips with regular expressions myself so its nice to hear other peoples thoughts



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum