Checking against regular expression on KeyPressEvent

This is a discussion on Checking against regular expression on KeyPressEvent within the C# Programming forums, part of the General Programming Boards category; Hi all, I'm trying to put something together that validates a textbox against a regular expression that defines currency, so ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    21

    Checking against regular expression on KeyPressEvent

    Hi all,

    I'm trying to put something together that validates a textbox against a regular expression that defines currency, so on each key press, if the text does not match a valid currency amount it handles that key press.

    Here's the code i'm using:

    Code:
    private void InputMoney(KeyPressEventArgs e, string s)
            {
                if (!Regex.IsMatch(s, @"^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$"))
                {
                    e.Handled = true;
                }
    When I run this code it misbehaves, allowing some numerical input but not all that you would relate to a currency format. For example if I enter "01." it will then not let me enter any more numbers, even though it should allow "01.00". Any ideas what I'm doing wrong?

    Thanks.
    Last edited by scott_ill; 10-26-2009 at 12:16 PM.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    21
    By the way, I nabbed the regular expression from regexlib.com, and it should "Match currency input with or without commas."

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Suppose you typed 01 into the textbox. If you then entered a decimal point '.' the exact text contained in the textbox would be "01.", which is not valid according to your regular expression.

    You might try tweaking the regex a bit:
    Code:
    Instead of:
    @"^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$"
    
    Maybe:
    @"^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9]{0,2})?$"
                                                   ^---------^
    That should allow an input of "01." or "01.1", or "01.11", up to two decimal places.

    As an alternative, instead of handling the key event and preventing the text from being entered, you might instead change the background color of the textbox to a color that indicates an error (Color.MistyRose maybe?)

    Then, in the future, you don't have to worry about a complicated regular expression locking out your ability to type in a valid string. Sometimes you may have to type in something invalid in order to get to a valid state.

    If, for example, you were to try the same approach with a phone number it probably wouldn't work. Surely you have to type something like "(368)" before you continue entering the rest of the phone number, but that obviously isn't a valid phone number on its own.

    You might also want to look at the MaskedTextBox, which can do the same thing, only it doesn't use regular expressions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 10:36 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 10:05 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21