Regular expressions and commas

Discussion in 'ASP .Net' started by Robert Scheer, Nov 22, 2004.

  1. Hi.

    I have a regularexpression validator control on a page. This regular
    expression validates a textbox to accept only numbers and commas:

    I am trying to modify this expression to not allow commas at the
    beginning and at the end of the expression without success. It needs
    to allow commas only between the numbers. How can I do that?

    Robert Scheer
    Robert Scheer, Nov 22, 2004
    1. Advertisements

  2. Hey Robert. How about this (untested) one?


    It's been a while since I've crafted a regex, but I believe that will
    allow for (1 or 2 digits, followed by a comma (optionally)), followed by
    0 to many groups of 3 digits, followed by a comma, followed by 3 digits.
    OR just 1 to many digits.

    The place to go, though, is They have pre-made
    regular expressions for a bevy of common scenarios.



    Scott Mitchell

    * When you think ASP.NET, think!
    Scott Mitchell [MVP], Nov 22, 2004
    1. Advertisements

  3. Robert Scheer

    Guest Guest

    How about this one:


    While working with regular expressions, you may find the following tool
    helpful :

    Tor BÃ¥dshaug
    tor.badshaug [//at\\]
    Guest, Nov 23, 2004
  4. Robert Scheer

    Peter Blum Guest

    It sounds like you want to validate decimal values with the thousand's
    separator (1,200,300.45) and discovered that CompareValidator
    (Operator=DataTypeCheck, Type=Decimal) doesn't handle the decimal places.

    The regular expression has its faults. Mostly, it cannot really determine if
    a valid number is entered. It just looks at string patterns. The
    CompareValidator actually parses the text and converts it into a decimal
    value. If that conversion fails, it reports an error. By converting it to
    decimal, it also allows comparisons like comparing to another number as
    offered by the RangeValidator and CompareValidator.

    Considering this, you may want to use a technique that converts to a decimal
    value. Here are some ideas:
    1. Use a CustomValidator and implement server side validation that uses's Decimal.Parse() method. Going further, write a client-side evaluation
    function that strips out all commas then uses javascript's parseFloat()
    var vVal = parseFloat(vText);
    if (isNaN(vVal))
    // its an error

    2. My replacement to Microsoft's validators, Professional Validation And
    More, automatically supports thousands separators in its own versions of
    RangeValidator and CompareValidator. It handles client side validation
    properly and works on many more browsers.
    ( It also provides a DecimalTextBox
    and CurrencyTextBox that filter keystrokes and reformats, inserting
    thousands separators if you like.

    --- Peter Blum
    Creator of "Professional Validation And More" at
    Peter Blum, Nov 23, 2004
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.