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:
Tooltip=”Please enter a valid loan amount.” Text=”*” />
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.
ErrorMessage=”Please enter a valid loan amount.”
Tooltip=”Please enter a valid loan amount.” Text=”*” runat=”server” />
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.
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…)
It’s briefer too. Thanks, Matt!
(Long time, no see…)