Regular expression for date

I was looking for the perfect regex to validate a date.

The examples I found on the internet where all not good enough. If you try validating 29/02/2010 it should give a fault but it didn’t so I created my own regex.

The best regular expression I can think of right now is this:

^(((0?[1-9]|[12]\d|3[01])[\/](0?[13578]|1[02])[\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|[12]\d|30)[\/](0?[13456789]|1[012])
[\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|1\d|2[0-8])[\/]0?2[\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|(29[\/]0?2[\/]((1[6-9]|[2-9]\d)
?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00|[048])))$

This will allow al dates like 01/01/2010 but will deal with the special ones like 29/02/2010

Leave a Reply

Your email address will not be published. Required fields are marked *