C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-26-2009, 11:11 AM   #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 11:16 AM.
scott_ill is offline   Reply With Quote
Old 10-26-2009, 11:12 AM   #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."
scott_ill is offline   Reply With Quote
Old 10-30-2009, 05:08 PM   #3
Registered User
 
Join Date: Jan 2008
Posts: 276
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.
arpsmack is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:54 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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