0

I found a very useful regular expression for testing format and content of a date field in a regex example site

BUT I get a validation when I put in dates older than 2000 and since this is a field for inputting date of birth you can see why it would be a problem. I am sure it is an easy fix but regular expressions intimidate me.

$('#txtDOB').blur(function() {
//$('span.error-keyup-5').remove();
var inputVal = $(this).val();
var dateReg = /^[0,1]?\d{1}\/(([0-2]?\d{1})|([3][0,1]{1}))\/(([1]{1}[9]{1}[9]{1}\d{1})|([2-9]{1}\d{3}))$/;
if(!dateReg.test(inputVal)) {
    alert('invalid date format: ' + inputVal);
}

I am not married to this solution so if you can suggest a better way please comment away.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
Michael BW
  • 1,070
  • 3
  • 13
  • 26
  • 4
    Regular expression are not the tool of choice for date validation (see [here](http://stackoverflow.com/questions/4290433/regular-expression-for-aspregularexpressionvalidator-with-format-mmddyy-leap-y/4291747#4291747) why not). They can be OK to check for "something that could be a date", but if you need to consider date ranges, month lengths, leap years etc, you're better off with a [date parser](http://code.google.com/p/datejs/wiki/APIDocumentation#parseExact). – Tim Pietzcker Nov 10 '11 at 16:23
  • this is not a particularly good regex, but I can find you one (specifically the numerous `{1}`'s are meaningless) – Code Jockey Nov 10 '11 at 16:26
  • why wouldn't you just create a javascript date object and use it's native functions? – Joseph Marikle Nov 10 '11 at 16:30
  • What date won't your RegEx work with? It looks as if it should accept dates after the year 2000. See - http://jsfiddle.net/GGM6h/ – ipr101 Nov 10 '11 at 16:38
  • @ipr101 - For example, that regex claims that February 29th, 2100 is valid, when it is not. (2100 is not a leap year.) – Phrogz Nov 10 '11 at 16:49

3 Answers3

6

Instead of testing if a string matches one or more formats that you think might be good dates, I would suggest instead asking JavaScript if it thinks it is a valid date:

function isValidDate(str){
  return !isNaN(new Date(str));
}

This assumes that you're going to accept what the user gives you in any of a variety of formats (e.g. the horrid US MM/DD/YYYY or the more sane ISO8601 YYYY-MM-DD). If instead you have a specific format you will only accept, then parse your string based on that, pull out the year/month/date, and then ask JavaScript if this is a valid date:

function isValidDate(year, month, date) {
  var d = new Date(year*=1, month-=1, date*=1, 12);    // noon to skip DST issues
  return d.getFullYear()==year && d.getMonth()==month; // wrong date->wrong month
}

You need to check that the year/month/date all match because new Date(2011,11,32) is accepted and interpreted as 2012-1-1.

See also: Javascript method to ensure that a date is valid

Community
  • 1
  • 1
Phrogz
  • 296,393
  • 112
  • 651
  • 745
2

There's a whole lot of mess there. First, eliminate all the {1}'s. That just means one instance, which is totally redundant. Also, a character class with one value is the same as the character itself. So, [1] becomes 1.

So, that leaves us with:

 /^[01]?\d\/(([0-2]?\d)|([3][01]))\/((199\d)|([2-9]\d{3}))$/

This is MM/DD/YYYY presumably. but the YYYY is just 199[0-9] and any year > 2000 and < 9999. Wow, that's a date range!

As a basic, try:

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

This gives a year range of 1000 - 2999. But as Tim said above, if you want really valid dates, you should use a specific date validator.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
Mike Ryan
  • 4,234
  • 1
  • 19
  • 22
1

If you need to parse date strings into dates then I would check out this library:

rtpHarry
  • 13,019
  • 4
  • 43
  • 64