ASP.Net - Supporting Currency Input
Posted: 13 May 2011 01:23 PM
::: 0 Comments
::: permaLink
Question:
I have an ASP.Net application that allows users to enter currency amounts, which are stored in the SQL Server database with the type money. How can I support input of USD currency values, including the dollar sign and thousands separators, within the context of ASP.Net databound controls such as ListView, and declarative data access controls such as SqlDataSource?
Answer:
There are a few little tricks you need to know to support currency input in this scenario.
1. The most important, magical trick I've found is to simply declare your parameter with no Type or DbType attribute.
Example:
2. Use a RegularExpressionValidator control with a really solid, well tested expression for validating the currency input The expression I've been using is applicable to US currency, so if you're coding for another culture, the advice in #1 still applies but you'll need a regex that applies to your specific currency format
The currency regex I use is below:
\$?-?([1-9]{1}[0-9]{0,2}(\,\d{3})(.\d{0,2})?|[1-9]{1}\d{0,}(.\d{0,2})?|0(.\d{0,2})?|(.\d{1,2}))$|^-?\$?([1-9]{1}\d{0,2}(\,\d{3})(.\d{0,2})?|[1-9]{1}\d{0,}(.\d{0,2})?|0(.\d{0,2})?|(.\d{1,2}))$|^(\$?([1-9]{1}\d{0,2}(\,\d{3})*(.\d{0,2})?|[1-9]{1}\d{0,}(.\d{0,2})?|0(.\d{0,2})?|(.\d{1,2})))$
The regex above will validate entries like: $3, $3.00, $3,000, and $3000.00 It's not perfect, because it will validate something like $3000[.] (a digit at the end without the following two digits). However, I've found that SQL Server ignores the trailing period, so it's not a serious issue. The regex was posted on StackOverflow.com by the user JohnM, at the following link:
http://stackoverflow.com/questions/354044/what-is-the-best-u-s-currency-regex
Finally, one additional word of advice: If you application has many areas where currency values can be entered or edited, centralize your Regex pattern inside of a resource file. That way, whenever you need to apply a RegularExpressionValidator control, you can obtain the pattern for use in the ValidationExpression property like so:
ID="TuitionRegularExpressionValidator
ControlToValidate="InputTextBox"
ErrorMessage="The value entered in the 'Tuition' field is not a valid USD currency value."
ValidationExpression="]]>"
runat="server" />
I am counting down to a full week off work beginning Friday, May 13th at 5:00 PM sharp. Oh yeah!
Filed under: development asp.net regex
Ode to Pilates - A Haiku
Posted: 18 Mar 2011 03:13 PM
::: 0 Comments
::: permaLink
I am now quite sure
Joe Pilates is Satan.
Damn, my stomach hurts!
Filed under: personal humor exercise
Playstation Network
Posted: 12 Dec 2010 01:30 PM
::: 0 Comments
::: permaLink
Filed under: gaming ps3
Nightmare Monkey
Posted: 05 Jul 2010 10:29 PM
::: 0 Comments
::: permaLink
This picture scares me horribly. Please, don't ever let me see this monkey in person.
Picture: ARDILES RANTE / BARCROFT MEDIA. Source: www.telegraph.uk
Filed under: humor animals
On Coding Life Away
Posted: 09 May 2010 01:30 PM
::: 0 Comments
::: permaLink
Will someone please buy me this shirt?
Sometimes, I embody what the pictured shirt says. Every few months or so, I become obsessed over some programming project and spend almost all of my free time working on it until it's done. That's how this weblog, and the website it sits on, came into being in the first place.
Is this a good way to work? I'm not sure. One one hand, single-minded focus can be handy, because it makes it easier for me to push through and complete large bodies of code. I tend to overdo caffeine and sacrifice too much sleep during my code-a-thons, though, and it becomes more difficult to communicate with other humanoids when I've got code on the brain constantly.
I manage to get a little balance in my life by simply going months and months without doing any techie-type stuff at home. It's a feast-or-famine approach, I guess, but there really is more to life than just the internet. Seriously. Someone told me that, and I believe it.
You'll see a new design for my website appear this summer, the details of which are still quite secret. I have a couple of smaller code project planned as well. However, I am devoting most of the summer to non-techie stuff, such as looking for a house, marksmanship, taking care of our awesome dogs (see: my flickr account), and some awesome summer concerts (Iron Maiden/Dream Theater in June, and also RUSH! in June).
Today, I will be getting some sun in my backyard... mandatory sun, as I must bring the rule of law to my yard with my mighty weed-trimmer. As the weeds are trimmed, so too must beer be consumed. If you're a home-coding geek, reading this post: How do you like to work? How do you achieve balance? Do you try to? Tell me.
Filed under: personal tech geek-love