Tuesday, February 9, 2010

Validating a Phone number field with Regular Expressions

Validating user input is a very common task when building a program with a user interface. While this task may appear daunting at first, it can actually be quite easy. Once you know of a good approach.

Regular expressions were born in the Unix world, but they can Be of great benefit anywhere. Regular expressions are complex and intimidating, but once you have mastered the basics they can be used in many tasks.

Let's look at a common usage of Regex: to validate user input. In the following code, we need to verify that the string phoneNumber contains a valid US phone number. First we need to verify that only numbers are imputed into the system.


private boolean IsUSPhoneNumber(string phoneNumber)
{
return Regex.IsMatch(phoneNumber, @"^(\d+)$");
}


Doing a quick test reveals that indeed a string of 123456789 is considered valid. However, so is 123 or 1234567890. Clearly we need to verify length of the string in addition to content. The following code will ensure this:


private boolean IsUSPhoneNumber(string phoneNumber)
{
return Regex.IsMatch(phoneNumber, @"^(\d{10})$");
}


While this is an improvement, there is still much more to consider. What if the user places the area code in parenthesis? This is a fairly common practice and should be accounted for.


private boolean IsUSPhoneNumber(string phoneNumber)
{
return Regex.IsMatch(phoneNumber, @"^([(]{0,1})(\d{3})([)]{0,1})(\d{7})$");
}


This code checks to see if there is zero or one parenthesis, 3 digits between the parenthesis and 7 digits after.

But, there are still other possibilities to consider; such as, what if the user uses white spaces to delineate the different sections of the phone number?


private boolean IsUSPhoneNumber(string phoneNumber)
{
return Regex.IsMatch(phoneNumber,
@"^([(]?)(\d{3})([)]?)([ ]?)(\d{3})([ ]?)(\d{4})$");
}


Or if periods are used?


private boolean IsUSPhoneNumber(string phoneNumber)
{
return Regex.IsMatch(phoneNumber,
@"^([(]?)(\d{3})([)-]?)([ ]?)(\d{3})([ -]?)(\d{4})$");
}


Or dashes?


private boolean IsUSPhoneNumber(string phoneNumber)
{
return Regex.IsMatch(phoneNumber,
@"^([(]?)(\d{3})([)-.]?)([ ]?)(\d{3})([ -.]?)(\d{4})$");
}



The final code snippet will validate most US phone number formats, in just 1 line of code. There are certainly other methods to validate a phone number, but can they be written in 1 line of code?

Thursday, February 4, 2010

Team Building Exercises

My current manager recently posed several questions to the team that he wanted candid responses too. He's new to the organization, but he seems genuine in his efforts to improve our process. Several of his questions have gotten me thinking, which I'll turn into blog posts over the course of the month. The first I wanted to discuss is about team building exercises.

My manager asked for suggestions for a team building exercise that we could do. He never explained what he hoped to get out of the exercise but I'm sure it is the generic "get to know each other, build team cohesiveness" mumbo jumbo. On previous team outings we've played laser tag and gone bowling. I believe the team building value in these activities was minimal and was mostly chosen for the entertainment value.

So, what activities could a development team do together in the sake of team building? Here's a few suggestions that I have, though I've not been able to put into practice.

One idea is team lunches. Getting everyone together in one room for an hour in a relaxing environment can be good for the team. The boss doesn't even have to pick up the tab (though it doesn't hurt). An alternative to this would be a lunch pitch-in, where everyone brings in something to eat.

Another alternative is to have a brown bag lunch every so often and select someone to present information on a specific topic. Perhaps a particular design pattern, or new technology, or a new programming technique could be demonstrated. Maybe there is some new internal software being developed that the team could benefit from learning about in a relaxed setting.

If your development projects are like mine, they could use some internal documentation. You can address this issue as a team building exercise by setting up a documentation tool, like a wiki, and ask the developers to submit articles to the wiki. So long as the productivity doesn't suffer you could even create a contest for the best written article, or the best diagram.

Estimation poker itself can be a fine team building tool as well, and it provides some useful output; Estimations for code changes that have been hashed out in a group. Some more information on estimation poker can be found here.

Finally, my current manager has started a new team building exercise with us, he gives us a series of hints about a person on our team and we have to guess who it is. We've done this a couple of times and is a great way to learn about each other.

What team building exercises have you attempted or thought of?

Tuesday, February 2, 2010

Writing my way to better health

I've made a few references to the PDA that I own. I've found it very handy to use in many places where a laptop is too bulky, like the car or exercise bike. The last six blog posts were either written or edited while on the exercise bike. This is great multitasking that would be difficult to do with a laptop, though there are items out there for this task.

There are a few down sides to working in this fashion. First, because I am spitting my focus between two tasks, one may suffer. I can usually keep a good 10 or 11 mph pace on the bike, but I've found myself going at a more leisurely 8 or 9 mph pace.

Second, the device lacks grammar and spell check. I'm forced to do my best on my own, or hold off checking until I transfer the post to the PC.

The device also lacks internet access so I must wait to do any research or programming until I am on my laptop.

Finally, while the device does its best to translate my chicken scratch hand writing into text, my writing is a bit illegible, especially when on the bike.

Despite these drawbacks, I am getting blog posts done and I'm getting some great exercise while doing it. Given the results, I will deal with drawbacks.