Can anyone tell me the regular expression format for "MM/DD/YY HH:mm:ss AM/PM" to use it in a regular expression validator, in asp.net 2.0
7 Answers
I highly recommend that you do not use regex for this. Instead, you should create your own validator (inherit BaseValidator) and use DateTime.TryParseExact to check that the value can be converted to DateTime.

- 2,801
- 1
- 25
- 36
-
but i want to validate this on client side,i should not go to server side and validate.And one more thing is if I use client click for the button, all other required validators are not working. Please suggest – Aug 22 '09 at 07:27
This worked for me
^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$

- 821
- 18
- 34
-
1great this worked for me as well, though after 4 years of posting it greatly helped. – N Khan Oct 04 '17 at 07:38
Old question but often asked.
RegEx for matching all dates, including leap years.
For DD-MM-YYYY format
^(?:31#(?:(?:0[13578])|(?:1[02]))#)|(?:(?:29|30)#(?:(?:0[1,3-9])|(?:1[0-2]))#)(?:(?:1[6-9]|[2-9]\d)\d{2})$|^(?:29#02#(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0[1-9])|(?:1\d)|(?:2[0-8]))#(?:(?:0[1-9])|(?:1[0-2]))#(?:(?:1[6-9]|[2-9]\d)\d{2})$
For MM-DD-YYYY format
^(?:(?:(?:0?[13578]|1[02])#31)\1|(?:(?:0?[1,3-9]|1[0-2])#(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2#29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))#(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$
Please change # in the reg ex with your desired date separator. For a global separator replace # with (/|-|.)
RegEx tested against the following dates and running successfully. Performance is also listed.
29-02-2000 ---> true
millis 2
29-02-2006 ---> false
millis 0
29-02-2001 ---> false
millis 0
30-02-2000 ---> false
millis 0
31-11-2000 ---> false
millis 0
29-13-2000 ---> false
millis 0
31-11-2000 ---> false
millis 0
31-07-2000 ---> true
millis 0
31-08-2000 ---> true
millis 1
31-12-2000 ---> true
millis 0
30-11-2011 ---> true
millis 0
17-07-2011 ---> true
millis 0
04-10-1987 ---> true
millis 0
01-01-1900 ---> true
millis 0
24-05-88 ---> false
millis 0
24-05-88 ---> false
millis 0
29-12-2011 ---> true
millis 0
31-12-2000 ---> true
millis 0
1-1-2010 ---> false
millis 0
1-1-99 ---> false
millis 0
1-01-99 ---> false
millis 0
01-1-99 ---> false
millis 0

- 14,781
- 7
- 57
- 74
For client-side (JavaScript) validation, you can use
^\d\d/\d\d/\d\d \d\d:\d\d:\d\d [AP]M$
which will check syntax (digits, spaces, separators), strictly, against your spec. If a match is obtained, you then need to check the ranges of the values entered (e.g. 1 <= month <= 12, etc.) in the subgroups for the returned match.
Use this site to test out the regex.

- 95,872
- 14
- 179
- 191
Check this one. It also validates leap year.
^(((0[13578]|1[02])[\/\.-](0[1-9]|[12]\d|3[01])[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((0[13456789]|1[012])[\/\.-](0[1-9]|[12]\d|30)[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((02)[\/\.-](0[1-9]|1\d|2[0-8])[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((02)[\/\.-](29)[\/\.-]((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm)))$
06/12/2014 12:45:56 AM // true
11-12-1965 06:04:34 PM // true
11/31/2015 11:40:00 AM // false
12-31-1945 01:38:09 PM // true
02/29/2012 09:04:10 AM // true [leap year]
02/29/2013 09:04:10 AM // false
06.12.2014 13:04:10 AM // false

- 9,109
- 3
- 38
- 47
(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d
Format is MM-DD-YYYY

- 4,202
- 12
- 55
- 77
Can you try this
(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/\d\d (0[0-9]|1[0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9]) [AM|PM]
Hope it solves your problem.

- 2,170
- 5
- 23
- 34