Take the 2-minute tour ×

I am trying to validate a date entered into a text box. There is an input mask on the textbox which forces input of xx/xx/xxxx. I am trying to use a regular expression validator to enforce that a correct date is entered. I am not skilled in RegEx bascially at all. My co-worker found this one on the internet but I can't really tell what it's doing.

Does this look right? Seems overly complicated...


Does anyone know of a less complex expression that essentially does what I need?

Thanks, ~ck in San Diego

share|improve this question
This has been addressed in this post. See if it helps. –  JYelton Aug 31 '09 at 20:13

9 Answers 9

Why not use one of the methods available in the System.DateTime namespace? You could use DateTime.TryParse() (edit: DateTime.TryParseExact() is probably the right suggestion) to accomplish the validation.

share|improve this answer
Agreed, let the framework pull the weight on this one. –  Matthew Vines Aug 31 '09 at 20:16
And DateTime.TryParseExact() may be more appropriate depending on your exact needs. –  Matthew Vines Aug 31 '09 at 20:17
What if I wanna make sure the the date is correct at the button click itself? –  Muhammedh Nov 17 '13 at 8:11
+1 dont reinvent the wheel. Additionally, regex should not be use for validating the parsed value of a string, only the form of the string, to ensure that you can parse it. If it parses a proper pattern, you should not need TryParse. –  Gusdor Nov 17 '13 at 8:17

You can use DateTime.TryParseExact:

DateTime dt;

bool isValid = DateTime.TryParseExact(
    out dt);
share|improve this answer

The above regular expression is correct for dd/mm/yyyy format. the expression is

share|improve this answer
I believe the question was "is there a better way"... –  iMortalitySX Nov 6 '12 at 19:35

Kettenbach had a problem. His co-worker suggested using regexs. Kettenbach then had two problems.

As others have said, use DateTime.TryParse or DateTime.TryParseExact on a custom validator and save yourself the nightmare that is regex :)

share|improve this answer
Regexes aren't inherently evil. They need to be used judiciously, though. They work great for a certain class of text parsing problems. –  TrueWill Sep 1 '09 at 0:36
oh I never said they were evil, per say... But they are on a level with cats. –  Sk93 Sep 1 '09 at 8:06
Please attribute the above statement to Jamie Zawinski. –  Jason Dec 17 '09 at 16:57

This would be correct regular expression to use for date format dd/mm/yyyy

^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$
share|improve this answer
Not exactly - you need to make the initial zero optional or it won't match single digit day values like 2/12/2012. ^(0?[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$ –  5arx May 17 '13 at 10:04

As an alternative, you can use CompareValidator instead of RegularExpressionValidator. It goes like this:

<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtIssueDate" ErrorMessage="Invalid Date Format" Type="Date" Operator="DataTypeCheck" Display="Dynamic" Text="*" ForeColor="Red" ValidationGroup="valGroup1"></asp:CompareValidator>

share|improve this answer

We can use a CustomValidator and use the override the ServerValidate method to check the TryParse!

share|improve this answer

This isn't really an answer, but couldn't you use DateTime.Parse or DateTime.TryParse to check that the date is correct?

Either that or use a DateTime control to make sure it's impossible to enter data that isn't a DateTime. There's lots of JavaScript out there on this subject.

share|improve this answer

Last answer is actually the correct way to do. Use DateTime.TryParse.


DateTime dt;
if(DateTime.TryParse(Textbox1.Text,out dt))
 Label1.Text = "Invalid date format";
share|improve this answer
I was writing actual code for it. –  wwd Dec 17 '09 at 16:52
TryParse actually returns true if the parse was successful, so your notification of invalid date format should be in the else of that condition. –  Gabriel Espinoza Sep 1 at 15:50

Your Answer


