© J R Stockton, ≥ 2011-12-25

JavaScript Date Validation by RegExp Only

Links within this site :-

This material should move into page JavaScript Date and Time 4 : Validation..

Validating a Date String with Just a RegExp

This seems a fairly unreasonable thing to do, except as an intellectual exercise. A validator which returns a Date Object is more generally useful.

Nevertheless, I have found Date validation in Javascript in the archives of Usenet news:comp.lang.javascript. It gives a validator for the years 1900 to 2000, DMY order. I have verified its RegExp.

I have also written and tested a RegExp to validate all dates YYYY-MM-DD, where the letters represent any decimal digit. In Firefox 3.6.17, it is faster than methods using a Date Object, but slower than the best method using just arithmetic and logical operations. The comparison methods start with three Numbers, and do not themselves handle or check the string form.

Mark Pawalek's RegExp

Found on 2011-05-29 in news:comp.lang.javascript archives :- Date validation in Javascript.

Newsgroups: comp.lang.javascript
From: m...@swipe.plc.uk (Mark Pawelek)
Date: 2000/11/08
Subject: Re: Date validation in Javascript

Regular expressions can be quite long and can check for all cases:
months with 28, 29, 30, 31 days and valid and invalid leap year dates.

...

var reDate =
 /^((29(-|\/)0?2(-|\/)(19(0[48]|[2468][048]|[13579][26])|2000))|(((3[01]|[12][0-9]|0?[1-9])(-|\/)(0?[13578]|1[02]))|((30|[12][0-9]|0?[1-9])(-|\/)(0?[469]|11))|(([12][0-8]|19|0?[1-9])(-|\/)(0?2)))(-|\/)(19\d{2}|2000))$/
The above is for years 1900 to 2000, D M Y order, 1 or 2 digit month and day, separator "-" or "/". Below, TextArea 1 has a slightly simplified version of that RegExp.

RegExp Script

Note, and time, functions SlowDate and FastDate.

RegExp Form

This will check agreement between a built-in or user-supplied validation function and a built-in or user-supplied validation RegExp ; one of the pair should be "Known-Good". It can also indicate their speeds.

If wanted, enter the body of your JavaScript function YourCode(y, m, d) { } :

Editable RegExps ; whitespace and ≤ ... ≥ are ignored


Test RegExp :   Pawelek :   TextArea 1 :   TextArea 2 :   TextArea 3 :

Comparison function :   GoodDate =
SlowDate :   FastDate :   KwikDate :   YourCode :
Check both to verify.   GoodDate() :   Test RegExp :   otherwise for timing.
Four-digit years, two-digit maxes
Years : -   Mmax   Dmax   Elim  



For each scan, all "<...>" and whitespace are removed from the selected textarea string, which is then the argument for RE = new RegExp(). Then RE.toString() is shown.

With a tested correct validator selected, press the "1" button to start accumulating times for averaging overhead or the selected GoodDate() or the selected Test RegExp or both.

Pawelek is a copy of the original, entered as JavaScript. It is the default RE.

TextArea 1 shows essentially the same code, with added whitespace, editable, but input dates must be "DD-MM-YYYY".

TextArea 2 shows my own initial RegExp, with whitespace, editable -
  •   Line 1, all years, months except February, days 01-30
  •   Line 2, all Februaries, days 01-28
  •   Line 3, all years, day 31
  •   Line 4, leap years for years not divisible by 100
  •   Line 5, leap years for years divisible by 100.
In each 400 years, the lines accept respectively (Y × M × D) :-
400 × 11 × 30, 400 × 1 × 28, 400 × 7 × 1, 4×(2+4×3+5×2) × 1 × 1, 1 × 1 × 1 dates;
total = 132000 + 11200 + 2800 + 96 + 1 = 146097 dates.

Assuming that the time taken by a line is approximately proportional to its length, the lines are clearly in the best order for accepting valid dates. But if the 8 digits of the dates could take all values from 00000000 to 99999999, then it would be better first to reject months above 12 and days above 31 (which I don't know how to do in one RegExp).

TextArea 3 is derived from TextArea 2 by reduction.

< space on offer >





Performance (out of date)

Using Chrome 11.0 in WinXP sp3 on P4/3G :-
TextArea 2 : Y 0000-9999, M 00-13, D 00-32, 9 seconds.



Home Page
Mail: no HTML
© Dr J R Stockton, near London, UK.
All Rights Reserved.
These pages are tested mainly with Firefox and W3's Tidy.
This site, , is maintained by me.
Head.