Coffee and Code

The Life and Times of Jeff Woodman

Home >> Tag: code

Entries Tagged with “code”

First Previous 1 2 Next Last  Displaying 1 - 5 of 10 entries.

ASP.Net - Supporting Currency Input

Posted: 05/13/2011 01:23 PM | permaLink

Tags: development asp.net regex code

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: <asp:parameter name="TuitionPerCredit" />

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:

<asp:regularexpressionvalidator>
   ID="TuitionRegularExpressionValidator
   ControlToValidate="InputTextBox"
   ErrorMessage="The value entered in the 'Tuition' field is not a valid USD currency value."
   ValidationExpression="<%$ Resources:CommonValidation, Currency %>"
   runat="server" />

I am counting down to a full week off work beginning Friday, May 13th at 5:00 PM sharp. Oh yeah!


On Coding Life Away

Posted: 05/09/2010 01:30 PM | permaLink

Tags: personal tech geek-love code balance

Picture of T-Shirt
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.


Script.aculo.us is Mir.aculo.us!

Posted: 02/17/2008 02:18 PM | permaLink

Tags: development props javascript code

scriptaculous.jpg Scriptaculous is an uber-cool set of javascript libraries geared toward rich, interactive user interface design. It's built on top of Prototype, another fantastic javascript framework,  and delivers a ton of easy-to-use, cross-browser effects, transitions, and other user interface tools.

I don't know why it took me so long to get into scriptaculous... maybe it's the do-it-yourself punker in me that avoids such conveniences (But why rewrite all of that code, when scriptaculous is well-written, robust, and free??) At any rate, I finally downloaded the libraries a few days ago while out searching for a good drag 'n drop script. Now, I feel like kicking my own ass for not downloading scriptaculous sooner!

The scriptaculous website has many usage examples, but I thought it was a little difficult to find specific usage examples; there were a couple of instances where I resorted to viewing the source code on the demo pages. All that aside, in many cases you can enable the scriptacular fanciness with one line of javascript (such as registering a draggable element on a page):

Not bad! And here's a quickie function that toggles between SlideUp and SlideDown:

function doSlide(id)
{
    var elem = document.getElementById(id);
    if (elem.style.display == "none")
        new Effect.SlideDown(id);
    else
        new Effect.SlideUp(id);
}

Script.aculo.us is, in my humble opinion, a worthwhile addition to any web developer or designer's arsenal.


Death by Coding

Posted: 04/01/2007 05:35 PM | permaLink

Tags: development blog training code

Well, first I'll have to say that my new weblogger is much closer to completion now, with mad props to the folks over at Interface Technical Training (Phoenix, AZ). I attended their ASP.Net class here in Santa Fe last week. Aside from the normal course materials (which were excellent), both instructors helped me out with some architectural issues I had with the weblogger application.

Anyway, the class got me totally pumped to code, so I've been up late all week doing that stuff. I's tired today...

Peace out to anonymousrobot (congrats on the new machine!) and impoverishedcook (Congrats on the new job!)


The Straight Dope

Posted: 03/24/2007 02:03 AM | permaLink

Tags: development personal humor asp.net blog code

So this teenage kid and his mammy were standin' in front of me in line at Walgr**ns. He says to his mom, 'I gotta start shaving. Clean-shaven men get all the best jobs.'

And here I am, lookin' all haggard with 3 days worth of stubble and camo pants on.... wild-ass hair... so I say to the mammy: 'Well, I guess I'm outta luck!' And the mom says - I love this part - 'Oh, you see, my son means a STRAIGHT job.'

Ho ho.

Anyway, I found this link at leftslipper's website while searching for how to bind an ObjectDataSource to an object factory. The article not only told me what I needed to keep my new weblogger project rolling, but offers an alternative way to handle objects: namely acquiring them rather than constantly creating and destroying them.

First Previous 1 2 Next Last  Displaying 1 - 5 of 10 entries.