15

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...

(^((((0[1-9])|([1-2][0-9])|(3[0-1]))|([1-9]))\x2F(((0[1-9])|(1[0-2]))|([1-9]))\x2F(([0-9]{2})|(((19)|([2]([0]{1})))([0-9]{2}))))$)

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

Kit
  • 20,354
  • 4
  • 60
  • 103
Hcabnettek
  • 12,678
  • 38
  • 124
  • 190
  • This has been addressed in [this](http://stackoverflow.com/questions/669741/date-regex) post. See if it helps. – JYelton Aug 31 '09 at 20:13

10 Answers10

35

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.

Donut
  • 110,061
  • 20
  • 134
  • 146
  • 4
    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 08: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 08:17
25

You can use DateTime.TryParseExact:

DateTime dt;

bool isValid = DateTime.TryParseExact(
    "08/30/2009",
    "MM/dd/yyyy",
    CultureInfo.InvariantCulture,
    DateTimeStyles.None,
    out dt);
bluish
  • 26,356
  • 27
  • 122
  • 180
dtb
  • 213,145
  • 36
  • 401
  • 431
7

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$
akjoshi
  • 15,374
  • 13
  • 103
  • 121
Radha
  • 71
  • 1
  • 1
  • 4
    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$ – immutabl May 17 '13 at 10:04
3

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>

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Yogi
  • 410
  • 4
  • 16
3

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

(^((((0[1-9])|([1-2][0-9])|(3[0-1]))|([1-9]))\x2F(((0[1-9])|(1[0-2]))|([1-9]))\x2F(([0-9]{2})|(((19)|([2]([0]{1})))([0-9]{2}))))$)
Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
vishal
  • 31
  • 1
2

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 :)

bluish
  • 26,356
  • 27
  • 122
  • 180
Sk93
  • 3,676
  • 3
  • 37
  • 67
1

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

Example:

DateTime dt;
if(DateTime.TryParse(Textbox1.Text,out dt))
{
 Label1.Text = "Invalid date format";
}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
wwd
  • 21
  • 1
  • 1
    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 01 '14 at 15:50
0

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.

bluish
  • 26,356
  • 27
  • 122
  • 180
Blue Toque
  • 1,775
  • 13
  • 24
0

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

Muhammedh
  • 484
  • 1
  • 11
  • 25
0
([0][1-9]|[1][0-9|][2][0-9]|[3][0-1])\/([0][1-9]|[1][0-2])\/[1-2][0-9][0-9][0-9]

for dd/mm/yyyy (year can be from 1000 to 2999)

or

(([0][1-9]|[2][0-9]|[3][0-1]|[1-9]|[1][0-9])/([0][1-9]|[1][0-2]|[1-9])/([1-2][0-9][0-9][0-9]|[0-9][0-9]))

which includes d/m/yy (e.g. 1/12/82)