Results 1 to 6 of 6

Thread: [RESOLVED] regularexpression for decimal

  1. #1
    Fanatic Member davebat's Avatar
    Join Date
    Dec 02
    Posts
    727

    Resolved [RESOLVED] regularexpression for decimal

    I currently have this regular expression for a whole number with optional decimal to 2 places.

    I need to chnage it so that the number has to be greater than 1.

    Can anyoen help

    Code:
    [0-9]+(\.[0-9][0-9]?)?

  2. #2
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 04
    Location
    The Granite City
    Posts
    21,729

    Re: regularexpression for decimal

    Hey,

    Wouldn't you just change the first 0-9 to be 1-9?

    However, what is wrong with using the regular expression to bring out the number, and then doing a normal > operation on the result?

    Gary

  3. #3
    Fanatic Member davebat's Avatar
    Join Date
    Dec 02
    Posts
    727

    Re: regularexpression for decimal

    Hi Gep, Thats the first thing I tried mate but that means no 0's would be allowed before the decimal.

    so 12.58 would pass but 10.58 wouldnt.

    I wanted to have this all done through the same regular expression validator ideally so I coudl avoid postback.

    Possibly something like [1-9][0-9]+(\.[0-9][0-9]?)?

  4. #4
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 04
    Location
    The Granite City
    Posts
    21,729

    Re: regularexpression for decimal

    Ah, so you are doing validation on the client...

    Couple of options, Range Validator, or a custom validator.

    Yes, your last option should do it, or at least get you closer. The only problem being whether you need a number higher than 99?

    Gary

  5. #5
    Fanatic Member davebat's Avatar
    Join Date
    Dec 02
    Posts
    727

    Re: regularexpression for decimal

    Yes it client side, I managed to work it out anyway, and learn something so was quite good.

    Code:
    ^[1-9]{1}([0-9]+)?(\.[0-9][0-9]?)?
    The ^[1-9]{1} means:
    ^ Start of string
    [1-9] character between 1 and 9
    {1} Limited to one


    Finally I wrapped the [0-9] in () and added ? after which means optional as this isnt mandatory anymore

  6. #6
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 04
    Location
    The Granite City
    Posts
    21,729

    Re: [RESOLVED] regularexpression for decimal

    Good stuff! Yes, regex can be fun

    A useful site to keep bookmarked is this one:

    http://gskinner.com/RegExr/

    Gary

Posting Permissions

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