Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Aug 2005
    Posts
    365
    Thanks
    7
    Thanked 1 Time in 1 Post

    onKeyPress JS alert

    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???

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Give this a try

    PHP Code:
    <script type="text/javascript">

    function 
    test(){
    str=document.getElementById("t1").value
    re
    =/bokb/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()"> 
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #4
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by david_kw View Post
    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
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •