I recently tried to validate a time input on an ASP.NET form and I didn’t manage to find a good regular expression on the net for the times I was trying to validate.  I call these “natural times” becaues it’s the way that most north americans write time. (12 hr clock, with : and whitespace separators). This won’t take UTC dates or 24 hr times that I know of but it works well (so far) with the .Tostring(”t”) short time representation of times.


((1+[012]+)|([123456789]{1}))(((:|\s)[0-5]+[0-9]+))?(\s)?((a|A|p|P)(m|M))


It seems longer than necessary (they always seem to), but it’s pretty forgiving for the user typing the time.I won’t take the time to explain all of the features of the expression but you can use the following links to help test it out.



  1. My favorite regular expression testing site.
  2. Regular expression reference site.

Regular expressions are one of those things that you run into often enough that you need to know them, but not often enough that you ever really get good at them.  So each time we struggle to get back in that mindset and then do some trial and error to get through the expression development.  I’m keeping this on my blog mostly for my own sake, so that I have it again when I need it. 


But feel free to lift it and give me feedback.