The Mighty Blog

Regular Expression - Validating Time

Apr 9

Written by:
4/9/2007  RssIcon

I am just an amateur when it comes to regular expression, but I plan on getting a whole lot better at it. There is too much potential there to not want to use it more often. I just came up with a regular expression pattern to validate time format. It has some small checks incorporated for valid times, but it is not 100% inclusive. Here is the expression:

^[0-1]?[0-9]{1}:[0-5]{1}[0-9]{1} (AM|PM|am|pm)$

The preceding pattern will match times such as:

12:29 PM, 12:29 pm, 2:14 AM, 2:14 am, etc.

It will not match:

22:29 PM, 11:69 AM, etc.

However, the catch here is that it will also match times like:

19:00 AM, 13:14 pm, etc.

It may or may not be obvious to you that this not a valid time, but the expression will not find it as obvious. Like I said, I am an amateur here, so let me know if you have a better expression for this.

Tags:
Categories: ASP.Net

1 comment(s) so far...


Gravatar

Re: Regular Expression - Validating Time

nice...

By spade on   8/20/2009

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
Add to Technorati Favorites
Tweet about my blog
The opinions expressed here are the personal opinions of Will Strohl and do not necessarily represent the views and opinions of the DotNetNuke Corporation.
© Copyright 2004-2011 by Will Strohl. All rights reserved. Website Skinned By: Ralph Williams  Website Hosted By: Applied Innovations