views:

68

answers:

4

I'm working in javascript and I need to figure out how to determine a valid date using regular expressions

matches will be:

dd-mm-yyyy
dd-mm-yy

Also no leading zeros should be accepted like:

9-8-2010
10-6-99

Can anyone help me out with this?

+2  A: 

Top answer on google: http://www.regular-expressions.info/dates.html

Nellius
that gives me the wrong format, it's dd-mm-yyyy not yyyy-mm-dd
Pieter888
You can't just search google and post the top result and expect to get +1
RobertPitt
at the bottom of the article he explains how to use it for matching dd-mm-yyyy
Am
@Robert, but in this case he's right, that was the solution...
Am
@RobertPitt I don't expect a +1, I expect the OP to realise that his question took two seconds on google.
Nellius
You should of put more explanation into your post IMO, 4 four words and a link seems a little idle thats all
RobertPitt
Or http://www.regexlib.com/REDetails.aspx?regexp_id=1019 . Its best to go with tested regexs and googling is probably the way to go.
Ravindra Sane
A: 

How about this?

[1-9]{1,2}[-./][1-12]{1}[-./](19|20)[1-99]{1}

I have not tested this. And this does not validate leap years

Bala
this will allow invalid dates, like 31-02-2010
Am
This one doesn't even work at all.
Pieter888
+2  A: 

You'd better do a split on - and test all elements. But if you really want to use a regex you can try this one :

/^(?:(?:31-(?:(?:0?[13578])|(1[02]))-(19|20)?\d\d)|(?:(?:29|30)-(?:(?:0?[13-9])|(?:1[0-2]))-(?:19|20)?\d\d)|(?:29-0?2-(?:19|20)(?:(?:[02468][048])|(?:[13579][26])))|(?:(?:(?:0?[1-9])|(?:1\d)|(?:2[0-8]))-(?:(?:0?[1-9])|(?:1[0-2]))-(?:19|20)?\d\d))$/

Explanation:

^            # start of line
 (?:         # group without capture
             # that match 31st of month 1,3,5,7,8,10,12
   (?:       # group without capture
     31      # number 31
     -       # dash
     (?:     # group without capture
       (?:   # group without capture
         0?  # number 0 optionnal
         [13578] # one digit either 1,3,5,7 or 8
       )     # end group
       |     # alternative
       (1[02]) # 1 followed by 0,1 or 2
     )       # end group
     -       # dash
     (19|20)? #numbers 19 or 20 optionnal
     \d\d    # 2 digits from 00 to 99 
   )         # end group
|
   (?:(?:29|30)-(?:(?:0?[13-9])|(?:1[0-2]))-(?:19|20)?\d\d)
|
   (?:29-0?2-(?:19|20)(?:(?:[02468][048])|(?:[13579][26])))
|
   (?:(?:(?:0?[1-9])|(?:1\d)|(?:2[0-8]))-(?:(?:0?[1-9])|(?:1[0-2]))-(?:19|20)?\d\d)
 )
$

I've explained the first part, leaving the rest as an exercise.

This match one invalid date : 29-02-1900 but is correct for any date between 01-01-1900 and 31-12-2099

M42
+1  A: 

I came up with this:

function isValidDate(inputDate){

    var myRegex = /^(\d{1,2})([\-\/])(\d{1,2})\2(\d{4}|\d{2})$/;
    var match = myRegex.exec(inputDate);

    if (match != null) {
        var auxDay = match[1];
        var auxMonth = match[3] - 1;
        var auxYear = match[4];
        auxYear = auxYear.length < 3 ? (auxYear < 70 ? '20' + auxYear : '19' + auxYear) : auxYear;
        var testingDate = new Date(auxYear,auxMonth,auxDay);
        return ((auxDay == testingDate.getDate()) && (auxMonth == testingDate.getMonth()) && (auxYear == testingDate.getFullYear()));
    } else return false;
}

Works for dd-mm-yyyy, dd-mm-yy, d-m-yyyy and d-m-yy, using - or / as separators

Based on This script

Adriana Villafañe