regular expression

Discussion in 'HTML' started by Joseph Wu, Aug 31, 2003.

  1. Joseph Wu

    Joseph Wu Guest

    <html>
    <head>
    <title>Project 5 Test</title>
    <style><!--
    body{font-size:18}
    input{font-size:24}
    --></style>
    <script language="JavaScript">
    <!--

    function SymError()
    {
    return true;
    }

    window.onerror = SymError;

    var SymRealWinOpen = window.open;

    function SymWinOpen(url, name, attributes)
    {
    return (new Object());
    }

    window.open = SymWinOpen;

    //-->
    </script>
    <script language="JavaScript"><!--
    function do_update() {
    var tx = document.myform.mytext.value;
    var re = /[0-9]{1,2}/;
    if (tx.match(re))
    document.bgColor = "lightgreen";
    else
    document.bgColor = "pink";
    }

    --></script>
    </head>
    <body onkeyup="do_update()">
    <form name="myform">
    <p><input type="text" size="60" name="mytext"> <br>
    Test Text </p>
    </form>
    <script language="JavaScript">
    <!--

    window.open = SymRealWinOpen;

    //-->
    </script>
    </body>
    </html>
    -----------------------------------------------------------
    i want to make all number with 2 digits valid, but
    why it keeps green if i type more than 2 digits?

    thanks in advance
     
    Joseph Wu, Aug 31, 2003
    #1
    1. Advertising

  2. Joseph Wu wrote:
    > i want to make all number with 2 digits valid, but
    > why it keeps green if i type more than 2 digits?

    If you don't anchor a regex to the start and end of text, it will match
    if the text contains the pattern. Try changing it to this:
    var re = /^[0-9]{1,2}$/;
     
    Leif K-Brooks, Aug 31, 2003
    #2
    1. Advertising

  3. Joseph Wu

    Joseph Wu Guest

    Leif K-Brooks <> wrote in
    news:OUd4b.479$2.webusenet.com:

    > Joseph Wu wrote:
    >> i want to make all number with 2 digits valid, but
    >> why it keeps green if i type more than 2 digits?

    > If you don't anchor a regex to the start and end of text, it will match
    > if the text contains the pattern. Try changing it to this:
    > var re = /^[0-9]{1,2}$/;
    >
    >


    thank you very much , it works !
     
    Joseph Wu, Aug 31, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith-Earl
    Replies:
    1
    Views:
    456
    Mary Chipman
    Jun 15, 2004
  2. VSK
    Replies:
    2
    Views:
    2,303
  3. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    851
    Alan Moore
    Dec 2, 2005
  4. GIMME
    Replies:
    3
    Views:
    11,972
    vforvikash
    Dec 29, 2008
  5. Noman Shapiro
    Replies:
    0
    Views:
    235
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page