Regular expression for dates without separator

Posted on

Question :

I have to know if a sequence of 8 numbers is a valid date in the format dd / mm / yyyy.
The closest I came to was using this expression:


But it is returning true for sequences that are not valid dates, for example:

39091991 (39/09/1991)
10181991 (10/18/1991)

I would also like the year to have a limit, 1990 - 2020 .

I’m not much with regular expressions, so if anyone can help me.


Answer :

Try this out






Only problem is that it does not valid February (30.02.2000) for example is valid.

You can validate dates with this exp, but there is no year limitation:



Although @AdirKuhn has already provided an acceptable solution to the AP. After seeing it, I went looking for something more powerful, which could at least validate months of 30 and 31 days, because I knew that this was possible, even though I knew it was laborious.

So I found this answer in SOen , which went beyond my expectations, since it even validates dates for leap years (in the case on the 29th of February).

Then after some adaptations the Portuguese language (I removed the 01/Feb/2015 option, only accepting months in numbers: 01/02/2015 ), I share this powerful regular expression to validate dates:


The expression flowchart for better understanding:

Online sample in the tool used for debugging and generating the regular expression flowchart (Debuggex)


Leave a Reply

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