1: /// <summary>
2: /// Validates the time against a regular expression that ensures standard military or
3: /// standard AM/PM time formats. Returns true or false based on the input time span.
4: /// </summary>
5: /// <param name="timeSpan"></param>
6: /// <returns></returns>
7: public static bool ValidateTime(this TimeSpan timeSpan)
8: {
9: bool validTime = false;
10: string tmpTime = timeSpan.ToString();
11:
12: Regex validTimePattern = new Regex(@"^((([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?))$");
13:
14: validTime = validTimePattern.IsMatch(tmpTime);
15: return validTime;
16: }
That’s the joy of RegEx. The old method was 50+ lines of code and it wasn’t reliable. Of course, you have to be familiar with RegEx in order to make a method like this work… or do you…?
Well, it turns out, you really don’t. I would be hard pressed to write a regular expression that was more complicated than, say, a simple 7 digit phone number. I certainly didn’t have the wherewithal to write the above monstrosity of RegEx. So, where did it come from? The answer is hardly surprising. I’ve had need of regular expressions before, and inevitably I always wind up going to the following site to get them:
RegEx Library
This place has a searchable interface, good categorization of expressions, and thousands upon thousands of regular expressions to choose from. Many times there are dozens of expressions that perform the same checks, or perform very similar checks. Unfortunately, that can cause trouble if you aren’t careful, so it’s always a good idea to double check your regular expressions before you put them into production. I do that with the help of this site:
JavaScript Regular Expression Editor
That site is a life saver. It’s very simple to use: Simply enter your Regular Expression in the top box, enter the string you wish to validate in the middle box, and observe your results in the bottom box. With a few minutes on this site, you’ll have a pretty good idea if your expression will work for you or not. Remember to test for things like case, length, and invalid characters. Once you have a regular expression that you are confident will work for your situation, you can use the code shown above to craft a very simple check to validate your input.
Rich