• Home
  • About Gregory Graham
  • Disclaimer

The Solarium

A Sunny Place for Ideas to Grow

Feeds:
Posts
Comments
« Pre-scoring Candidates for String Matching
Farkle Odds »

ASP.Net: Validating a TextBox with a Dollar Sign

August 26, 2009 by ggraham412

Currency Parsing, Regular Expressions

ASP.Net 2.0 has some very powerful client-side web page validation features, including classes that emit javascript validation code to the user’s web browser.  The CompareValidator, allows one to test whether the value entered into a text box is convertible to a given data type.  All one has to do add the CompareValidator tag to your markup,  set the properties and ASP.Net does the rest.  But don’t expect it to handle a dollar sign: use a regular expression for that!


For example, to check if a valid monetary amount is entered in a text box named txtLoanAmount:



When a user submits a form containing this code, javascript emitted by the CompareValidator will check the value entered into the txtLoanAmount text box on the client side. If the value entered is not convertible to a currency amount, then it will display a “*” next to the text box and a more detailed error message in the ValidationSummary control and in a tooltip. (Always remember to also check Page.IsValid server side since client-side validation can be spoofed!)

But for some reason it doesn’t work with a dollar sign. When the data type checked is “Currency” (in en-US culture), it works for any numeric input that includes comma and decimal formatting only. Validation fails(!?!) when the leading non-whitespace character is a dollar sign. I am not sure about other cultures. It is surprising to me, but there it is. So if you want to allow a dollar sign in your validated input, you should use a RegularExpressionValidator instead.

The regular expression given above (crafted in only a few moments) will accept an optional “$” surrounded by whitespace, at least one “leading” field of 1, 2, or 3 digits, and zero or more trailing fields with exactly three digits and optional comma separator. Finally, there is an optional decimal field with two training digits.

The strategy above will work for other cultures on a one-off basis. Unfortunately, the given regular expression will work for en-US culture only. And you can’t apply the regular expression through a Skin file. Other cultures will need different regular expressions here, and so it looks like some additional strategy would have to be developed to validate currency amounts across cultures.

Like this:

Like
Be the first to like this post.

Posted in ASP.Net, Programming | Tagged linkedin | 2 Comments

2 Responses

  1. on September 4, 2009 at 5:22 pm Matt Meyer

    Hey Greg,

    It’s been a while. I stumbled on this from Facebook via work. How’ve you been?

    Anyhoo, there’s a bug in your regular expression (or a feature) where it won’t validate the digit grouping until it hits a group (IE, $25535,523.00 will validate). You should give this regex a try:

    ^\s*\$?\s*\d{1,3}((,\d{3})*|\d*)(\.\d{2})?\s*$

    This allows for either no digit groupings, or enforces the groupings across the entire value if any are encountered. I’ve only tested this with perl, but .Net should work with it (in theory…)


  2. on September 4, 2009 at 6:19 pm ggraham412

    It’s briefer too. Thanks, Matt!

    (Long time, no see…)



Comments are closed.

  • Recent Posts

    • Using the Internal Keyword While Preserving Encapsulation
    • Farkle Odds
    • ASP.Net: Validating a TextBox with a Dollar Sign
    • Pre-scoring Candidates for String Matching
    • String Matching and Zaxxon
  •  

    August 2009
    M T W T F S S
    « Oct   Oct »
     12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31  
  • a

  • Archives

    • October 2009
    • August 2009
    • October 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • October 2007
    • July 2007
    • May 2007
    • April 2007
    • March 2007
  • Blog Stats

    • 107,155 hits
  • Meta

    • Register
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.com

Blog at WordPress.com.

Theme: MistyLook by Sadish.


Follow

Get every new post delivered to your Inbox.

Powered by WordPress.com