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:
^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2)(?:(?:(?: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})$
The expression flowchart for better understanding:
Online sample in the tool used for debugging and generating the regular expression flowchart (Debuggex)