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

Numeric Validation

  •  01-28-2008, 9:48 PM

    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: , , ,
View Complete Thread