Regex for dd/mm/yyyy and d/m/yyyy [duplicate]
Possible Duplicate:
Regex to validat开发者_开发问答e dates in this format d/m/yyyy
I can find regex for dd/mm/yyyy but does anyone know what it would be to also allow single digit dates and months (e.g. d/m/yyyy)?
I think below is work for you
^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]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)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$
if you've got a working regex for dd/mm/yyyy
, then just add a question mark after the first d
and m
characters to make them optional.
However, I would caution that date valiation using regex does have issues -- you might find it more flexable to use a dedicated date validation library (you don't specify what language you're working with, but most languages do have good date handling tools available)
I think you could use this
(\d{1,2})/(\d{1,2})/(\d{4})
Working example: http://regexr.com?2up1s
^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1?[012])/(19|20)\d\d$
(?x)
(0?[1-9]|[12][0-9]|3[01])
/
(0?[12]|1[0-2])
/
[0-9]{4}
精彩评论