“32/1/2010” , “32/01/2020” – day is out of range [1-31] 2.

You want to validate dates in the traditional formats mm/dd/yy, mm/dd/yyyy, dd/mm/yy, and dd/mm/yyyy.

“29/2/2007”, “29/02/2007” – 2007 is not leap year, only has 28 days 4.

regular expression for validating date format ddmmyyyy-85

To match a date in the format 'yyyy-MM-dd hh:mm' (Or indeed in whatever order you please) ^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$ I suspect that the following is as accurate as can be expected without knowing when the user's locale switched over from the Julian to the Gregorian calendars.

Unfortunately, all it does is match valid numbers in a date time input, and 31st Feb will be marked as valid, but as so many have said, regex really isn't the best tool to do this test.

[1-9] # 01-09 or 1-9 | # .[12][0-9] # 10-19 or 20-29 | # .3[01] # 30, 31 ) #end of group #1 / # follow by a "/" ( # start of group #2 0?

[1-9] # 01-09 or 1-9 | # .1[012] # 10,11,12 ) # end of group #2 / # follow by a "/" ( # start of group #3 (19|20)\\d\\d # 19[0-9][0-9] or 20[0-9][0-9] ) # end of group #3 The above regular expression is used to validate the date format in “dd/mm/yyyy”, you can easy customize to suit your need.

“31/4/20100”, “31/04/2010” – April has 30 days only 8.

“333/2/2008”, “29/02/200a” – day is invalid, year is invalid 7.

We use alternation (see Recipe 2.8) inside a group to match various pairs of digits to form a range of two-digit numbers.

We use capturing groups here because you’ll probably want to capture the day and month numbers anyway.

If you already know your subject text doesn’t contain any invalid dates, you could use a trivial regex such as cannot repeat more than two times or fewer than two times.

Solutions 3 through 6 restrict the month to numbers between 1 and 12, and the day to numbers between 1 and 31.

: # day-check: non caturing group # days 01-28 0[1-9]|1\d|[2][0-8]| # february 29d check for leap year: all 4y / 00 years: only each 400 # 0400,0800,1200,1600,2000,...