How can we check the date in ddmmyyyy format (including leap year validation) using regular expression? What would be the regular expression...?
Asked
Active
Viewed 8,640 times
1
-
2Did you try something already? – Juru Oct 14 '14 at 08:27
-
**Can you do this without regular expression?** It would be a mess to maintain. – nhahtdh Oct 14 '14 at 08:28
-
1Have you considered the possibility, that regex may not be the right solution to that problem? – Danubian Sailor Oct 14 '14 at 08:29
-
1Have a look at: http://stackoverflow.com/a/3873172/372239 – Toto Oct 14 '14 at 08:33
-
I'm trying this in angularJS, I've to write another directive for this validation – Gangadhar Jannu Oct 14 '14 at 08:36
-
@M42 I've already searched over google about validation but I want validation for date in ddmmyyyy format. Can you show me the duplicated one I haven't found my answer in the link provided by you – Gangadhar Jannu Oct 14 '14 at 08:41
-
The answer I gave does exactly ddmmyyyy, but @M42's link is a very similar approach and it shouldn't be too hard for anyone to adapt it and remove the separator. You are sounding very "give me ze code" and "do my work for me" when insisting in multiple comments that you only want something that works for exactly ddmmyyyy. – asontu Oct 14 '14 at 08:44
-
@funkwurm I know the stackoverflow rules man however I've tried similar approaches by removing seperators (-, ., /) but I was unable to find the correct one. Anyway thanks for the answer. – Gangadhar Jannu Oct 14 '14 at 08:51
2 Answers
13
Dude, you asked the question I've been working on for a couple of weeks. I invite those commenting to give a date that breaks this. Now note that this works for the years 1000-9999, is Proleptic Gregorian and assumes that we won't change how leap-years work until the year 9999 ;)
^(?:(?:(?:0[1-9]|1\d|2[0-8])(?:0[1-9]|1[0-2])|(?:29|30)(?:0[13-9]|1[0-2])|31(?:0[13578]|1[02]))[1-9]\d{3}|2902(?:[1-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00))$

asontu
- 4,548
- 1
- 21
- 29
-
1
-
-
Fails for October 10/29 and 10/30 of each year. So there's that. – Michael Paulukonis Feb 19 '19 at 16:12
-
@MichaelPaulukonis [works perfectly fine](https://regex101.com/r/qyko17/1)? Beware that this is `ddmmyyyy` not `mmddyyyy` as your 10/29 and 10/30 are. – asontu Feb 27 '19 at 15:35
0
Regex for dd/MM/yyyy
even for leap year:
pattern="(^(((0[1-9]|[12][0-8])[\/](0[1-9]|1[012]))|((29|30|31)[\/](0[13578]|1[02]))|((29|30)[\/](0[4,6,9]|11)))[\/](19|[2-9][0-9])\d\d$)|(^29[\/]02[\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)"
Or simple
pattern= "^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)$"

nhahtdh
- 55,989
- 15
- 126
- 162

FaheemFayaz
- 43
- 8
-
1Hi thanks for your answer however I want date validation for ddmmyyyy not dd/MM/yyyy format – Gangadhar Jannu Oct 14 '14 at 08:36
-
pattern= "^(0?[1-9]|[12][0-9]|3[01])(0?[1-9]|1[012])((19|20)\\d\\d)$"; – FaheemFayaz Oct 14 '14 at 08:40
-
-
1