javascript - Verify date with regular expressions -


this question has answer here:

date format 2015/04/25 trying

^\d{4}\/\d{2}\/\d{2}$ 

and working fine, want validate month should b less 12 , date less 31

i had tried this

^\d{4}\/(\d{2}\<[12])\/\d{2}$ 

but not working.

ps: noob in regular expressions.

you can use

^\d{4}\/(0\d|1[0-2])\/([0-2]\d|3[01])$ 

explanation:

  • \d{4} 4 digits
  • (0\d|1[0-2]) 0(any digit) or 1(0 2) i.e 00 09 or 10-12
  • ([0-2]\d|3[01]) (0 2)(any digit) or 3(0 or 1) i.e 00 29 or 30 or 31

edit1: if want match 01-12 months , 01-31 day (without 00) can use :

^\d{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])$ 

edit2: if want strict validation of dates use explode , checkdate.. suggested @wayne.. since includes validation of leap years.

see demo


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -