Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Numeric Validation

Last post 01-29-2008, 6:42 PM by jamesgileslang. 2 replies.
Sort Posts: Previous Next
  •  01-28-2008, 9:48 PM 39020

    Numeric Validation

    HI
    I am new to regular expressions BUT

    I am trying to create a regex that will validate numeric input where the number can be

    zero (0), positive (123), negative (-123), a decimal positive(123.123), a decimal negative (-123.123)

    but the decimal could be up to 8 places (123.12345678) 

    There are lots of examples that do part of what I require but none I could find (only looked for about an hour)
    That do it all

    so far this what I came up with 
    "^-{0,1}[0-9]+$|^-{0,1}\d*\.\d{0,8}$"

    and it works OK(ish)

    These are my limited tests so far

     
    "000.123" pass ( but I would rather it didn't)
    "1" pass
    "-1" pass
    "1.12345" pass
     "1.123466" pass
    "-1.254687" pass
    "-1.123456789" 'fail
    "-1.1234567" pass
    "james" 'fail

     The killer is the "000.123"
    I know most if not all development Environments will turn that  000.123 to 0.123 when  cast to a  decimal datatype
    but I would like my regex to flag it as invalid so the UI can be informed of an invalid entry

    (I am not looking for help on the UI stuff just how to code a regex to allow what I want....and invalidate what I don't)

    I am sure its quite simple but it eludes me 


    All help and Suggestions 
    Are appreciated

    Cheers

    James Lang 

    Filed under: , , ,
  •  01-29-2008, 4:31 PM 39064 in reply to 39020

    Re: Numeric Validation

    See if this works for you:

    ^-?(?!0{2,})\d+(?:\.\d{0,8})?$


  •  01-29-2008, 6:42 PM 39075 in reply to 39064

    Re: Numeric Validation

    Hi

    Thank you very much, that seems to be exactly what I wanted

     Cheers

    James

     

View as RSS news feed in XML