Reg exp not working...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kim

    Reg exp not working...

    Hello,

    I want valid input to be 1 to 9 digits followed by an optional decimal
    point, followed by an optional 1 to 4 digits. I think this should
    work:

    re = /\d{1,9}\.?\d{1, 4}/

    The problem I am having is that it is not restricting the number of
    digits to the right and left of the decimal point. So my "range" parts
    ({1,9} and {1,4}) are probably wrong in some way but I cannot figure
    out how... I have researched the syntax and this seems to make sense to
    me. Please help!

    Thanks,

    Kim

  • Michael Winter

    #2
    Re: Reg exp not working...

    On 21/10/2005 16:26, Kim wrote:

    [snip]
    [color=blue]
    > re = /\d{1,9}\.?\d{1, 4}/
    >
    > The problem I am having is that it is not restricting the number of
    > digits to the right and left of the decimal point.[/color]

    Only the decimal point is optional, not the decimal part (which is the
    intent, I assume). It also only needs to match a substring. Additional
    characters, including non-numeric ones, can appear before and after the
    matched pattern.

    /^\d{1,9}(\.\d{1 ,4})?$/

    The ^ and $ assertions ensure that the pattern matches characters at the
    start and end of the string, respectively. In other words, the entire
    string must match the pattern.

    [snip]

    Hope that helps,
    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.

    Comment

    • Kim

      #3
      Re: Reg exp not working...

      I realized I was missing the second ? after I posted. Thanks so
      much!!! That works perfectly! :-)

      Comment

      • Evertjan.

        #4
        Re: Reg exp not working...

        Kim wrote on 21 okt 2005 in comp.lang.javas cript:
        [color=blue]
        > I want valid input to be 1 to 9 digits followed by an optional decimal
        > point, followed by an optional 1 to 4 digits. I think this should
        > work:
        >
        > re = /\d{1,9}\.?\d{1, 4}/[/color]


        result = /\d{1,9}\.?\d{1, 4}/.test(myString)

        this would accept 1-9 digits + a optional . + 1 to 4 digits

        anyware in a long string.

        "qwerty12345678 91234qwerty" would be accepted.
        [color=blue]
        > The problem I am having is that it is not restricting the number of
        > digits to the right and left of the decimal point. So my "range" parts
        > ({1,9} and {1,4}) are probably wrong in some way but I cannot figure
        > out how... I have researched the syntax and this seems to make sense to
        > me. Please help![/color]

        Try:

        function result(x){
        return /^\d{1,9}(\.\d{1 ,4})?$/.test(x)
        }

        alert(result('1 2')) //true
        alert(result('q werty12')) //false
        alert(result('1 2.23')) //true
        alert(result('1 2.')) //false
        alert(result('. 23')) //false
        alert(result('0 .23')) //true
        alert(result('0 00000000.2300') ) //true ????

        This what you want?

        =============== =

        or without accepting starting (except in 0.23) and final zeros:

        function result(x){
        return /^(([1-9]\d{0,8})|0)(\.\ d{0,3}[1-9])?$/.test(x)
        }

        alert(result('1 2')) //true
        alert(result('q werty12')) //false
        alert(result('1 2.23')) //true
        alert(result('1 2.')) //false
        alert(result('. 23')) //false
        alert(result('0 .23')) //true
        alert(result('0 00000000.2300') ) //false !!!!


        --
        Evertjan.
        The Netherlands.
        (Replace all crosses with dots in my emailaddress)

        Comment

        • Kim

          #5
          Re: Reg exp not working...

          I am using: /^\d{1,9}(\.\d{1 ,4})?$/

          Thank you for you quick and effective responses!

          :-)

          Kim

          Comment

          Working...