0

I need a regular expression to validate date (including leap year) in MMM dd yyyy format
I found a regular expression, but it's currently bug at February 29 every leap year

^(?:(((January|March|May|July|August|October|December) 31)|((January|March|April|May|June|July|August|September|October|November|December) (([0-2]\\d)|30))|(February ([01]\\d|2[0-8])))|(February 29(?=-((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))) ((1[6-9]|[2-9]\\d)\\d{2})$

Please help me to fix it.
Thank in advance

Dranix
  • 551
  • 4
  • 11
  • 21
  • 3
    First question that comes to me is: WHY do you need regexp? Isn't there a class that can validate a date available? Don't reinvent the wheel. And BTW your regexp will accept April 00 6787. Do you really want to validate with regex that if Feb 29, then `year%4==0 && (year%100 != 0 || year%400==0)`? – Benoit Mar 02 '12 at 10:54
  • do you use the jquery date picker? – Jibi Abraham Mar 02 '12 at 10:59

4 Answers4

2

Could you try with this code instead:

"^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?|Dec(ember)?)\\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|Sep(tember)?|(Nov|Dec)(ember)?)\\ (0?[1-9]|([12]\\d)|30))|(Feb(ruary)?\\ (0?[1-9]|1\\d|2[0-8]|(29(?=,\\ ((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))))\\,\\ ((1[6-9]|[2-9]\\d)\\d{2}))"
IndieTech Solutions
  • 2,527
  • 1
  • 21
  • 36
Guillermo
  • 21
  • 1
2

Here's a function to check the validity of a date string, considering the format also:

function chkdate(datestr,formatstr){
    if (!(datestr && formatstr)) {return false;}
    var splitter = formatstr.match(/\-|\/|\s/) || ['-']
       ,df = formatstr.split(splitter[0])
       ,ds = datestr.split(splitter[0])
       ,ymd =[0,0,0]
       ,dat;
    for (var i=0;i<df.length;i++){
            if (/yyyy/i.test(df[i])) {ymd[0] = ds[i];}
       else if (/mm/i.test(df[i])) {ymd[1] = ds[i];}
       else if (/dd/i.test(df[i])) {ymd[2] = ds[i];}
    }
    dat = new Date(ymd.join('/'));
    return !isNaN(dat) && Number(ymd[1])<=12  && dat.getDate()===Number(ymd[2]);
}
//usage (in your case)
var months = {january:1,february:2,march:3,april:4,may:5,
              june:6,july:7,august:8,september:9,october:10,
              november:11,december:12},
inputdateraw  =  'february 29 2011'.split(' '),
inputdate = [months[inputdateraw[0]],
             inputdateraw[1],
             inputdateraw[2]].join(' ');
console.log(chkdate (inputdate, 'mm dd yyyy')); //=> false

See also this SO-question

Community
  • 1
  • 1
KooiInc
  • 119,216
  • 31
  • 141
  • 177
1

You can use moment js library, which provides powerful built in functions. Refer this fiddle for working example.

var date = "Mar 23 2001"; 
var indate = "Feb 29 2001";

moment(date,"MMM DD YYYY").isValid(); // true
moment(indate,"MMM DD YYYY").isValid(); // false
Saba
  • 3,418
  • 2
  • 21
  • 34
1

If you are using jquery then you could use the date picker that comes with it.

Or you could use the validation plugin. You could customize it by using addMethod function.

$.validator.addMethod(
    "australianDate",
    function(value, element) {
        // put your own logic here, this is just a (crappy) example
        return value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/);
    },
    "Please enter a date in the format dd/mm/yyyy"
);

And then on your form:

$('#myForm')
    .validate({
        rules :
            myDate : {
                australianDate : true
            }
    });

See here

Also there is a javascript date library - Datejs

Community
  • 1
  • 1
John Eipe
  • 10,922
  • 24
  • 72
  • 114
  • I'm using addMethod function too and I need a regex to validate what user input by hand, not by datepicker. Datejs is very cool site. Thank for your answer. – Dranix Mar 03 '12 at 16:35
  • using DateJS, you can just attempt to parse the string value. If a Date object is returned, you have a valid Date. If 'null' is returned, the string could not be converted into a Date object. Example... var isDate = (Date.parse(value) === null); – geoffrey.mcgill Mar 04 '12 at 01:26