0

Possible Duplicate:
Regex to validate 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)?

Community
  • 1
  • 1
Mike Poole
  • 1,958
  • 5
  • 29
  • 41
  • Can you post any attempts you have made. The RegEx for this is really trivial. I see a related topic with the anaswer you seek. – Security Hound Sep 23 '11 at 13:35

5 Answers5

6

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))))$ 
JD Varu
  • 216
  • 1
  • 2
  • 9
3

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)

Spudley
  • 166,037
  • 39
  • 233
  • 307
1

I think you could use this

(\d{1,2})/(\d{1,2})/(\d{4})

Working example: http://regexr.com?2up1s

Jason Gennaro
  • 34,535
  • 8
  • 65
  • 86
1
^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1?[012])/(19|20)\d\d$
Mircea Soaica
  • 2,829
  • 1
  • 14
  • 25
0
(?x)
(0?[1-9]|[12][0-9]|3[01])
/
(0?[12]|1[0-2])
/
[0-9]{4}
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125