-1

I want to validate the format of the date value entered by a user using regex with javascript.

My regex doesn't allow the '/' character , /[^0-9\.]/g,''

But I want to let '/' pass the regex test too. What modification do I need to make here?

punkrockbuddyholly
  • 9,675
  • 7
  • 36
  • 69
rawat0157
  • 13
  • 1
  • 1
  • 10

4 Answers4

2

Modified from this answer you can be pretty exact with this. 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}|29/02/(?:[1-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00))$

Regular expression visualization

Debuggex Demo

Community
  • 1
  • 1
asontu
  • 4,548
  • 1
  • 21
  • 29
  • Nice Twist with the leapyear and ofcourse through the image it is very easy to follow – winner_joiner Apr 14 '15 at 11:20
  • @winner_joiner yeah I'm a huge fan of the debuggex visualizations. It's a huge help when dealing with monstrous regexes like this. If you click somewhere in the regex text on the website it actually shows where in the visual you are, which is how I could find where to add the `/` everywhere quickly. – asontu Apr 14 '15 at 12:15
  • Awesome job! I just spotted one bug - it do not match `10` as a day, because there is pattern `[01][1-9]`. So we need one more case `(?:0[1-9]|1\d)` – [Debuggex Here](https://www.debuggex.com/r/c59u1dAfNc0879AW) – Rudolf Gröhling Apr 14 '15 at 16:23
  • @Vaclav nice find! I added it but without the additional grouping :) – asontu Apr 15 '15 at 07:07
1
"20/11/1992".match(/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/)

The above snippet should do, but there are too many validations to be performed on dates, so I wouldn't recommend regex.

Instead, I'd say do it like most websites do and place 3 combo boxes (dd/mm/yyyy), and allow the user to select a date, then you validate that date using the Date() constructor (if the values haven't changed, the date is correct).

note: the answer is based upon the assumption that you don't want to use any of the existing libraries (or the native validation provided by browser when using input[type="date"])

0

You can use this regex:

/(^(((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)$)/

This validates the date with format dd/mm/yyyy and also checks for leap years.

Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
0

It depends on how strict you need to be? I thing that simple:

/[0-3]\d\W[01]\d\W(?>19|20)\d{2}/g

should be sufficient.

day: [0-3]\d 2 digits, first 0-3, second any number \d

month: [01]\d 2 digits, first 0 or 1, second any number

year: (?>19|20)\d{2} 4 digits, starts with 19 or 20 (for 19th and 20th century) and next any digit two times {2}

Also note, I used \W to match single non-word character as workaround to match /. Are you sure that you cannot use escaped slash \/ instead?

Rudolf Gröhling
  • 4,611
  • 4
  • 27
  • 37