Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: how to validate dates in format MM-DD-YYYY?

I saw a potential answer here but that was for YYYY-MM-DD: JavaScript date validation

I modified the code code above for MM-DD-YYYY like so but I still can't get it to work:

String.prototype.isValidDate = function()  {      var IsoDateRe = new RegExp("^([0-9]{2})-([0-9]{2})-([0-9]{4})$");      var matches = IsoDateRe.exec(this);      if (!matches) return false;      var composedDate = new Date(matches[3], (matches[1] - 1), matches[2]);      return ((composedDate.getMonth() == (matches[1] - 1)) &&       (composedDate.getDate() == matches[2]) &&       (composedDate.getFullYear() == matches[3])); } 

How can I get the above code to work for MM-DD-YYYY and better yet MM/DD/YYYY?

Thanks.

like image 966
Sajee Avatar asked Nov 09 '08 21:11

Sajee


People also ask

How do you check if the date is in dd mm yyyy format in JavaScript?

The toISOString() method returns a string formatted as YYYY-MM-DDTHH:mm:ss. sssZ . If the ISO representation of the Date object starts with the provided string, then the date string is valid and formatted as DD/MM/YYYY .

How do you check whether a date is valid or not in JavaScript?

Store the date object in a variable. If the date is valid then the getTime() will always be equal to itself. If the date is Invalid then the getTime() will return NaN which is not equal to itself. The isValid() function is used to check the getTime() method is equal to itself or not.

How do you check if a date is valid?

Given date in format date, month and year in integer. The task is to find whether the date is possible on not. Valid date should range from 1/1/1800 – 31/12/9999 the dates beyond these are invalid. These dates would not only contains range of year but also all the constraints related to a calendar date.


2 Answers

function isValidDate(date) {     var matches = /^(\d{1,2})[-\/](\d{1,2})[-\/](\d{4})$/.exec(date);     if (matches == null) return false;     var d = matches[2];     var m = matches[1] - 1;     var y = matches[3];     var composedDate = new Date(y, m, d);     return composedDate.getDate() == d &&             composedDate.getMonth() == m &&             composedDate.getFullYear() == y; } console.log(isValidDate('10-12-1961')); console.log(isValidDate('12/11/1961')); console.log(isValidDate('02-11-1961')); console.log(isValidDate('12/01/1961')); console.log(isValidDate('13-11-1961')); console.log(isValidDate('11-31-1961')); console.log(isValidDate('11-31-1061')); 

It works. (Tested with Firebug, hence the console.log().)

like image 132
PhiLho Avatar answered Sep 17 '22 01:09

PhiLho


function isValidDate(date) {         var valid = true;          date = date.replace('/-/g', '');          var month = parseInt(date.substring(0, 2),10);         var day   = parseInt(date.substring(2, 4),10);         var year  = parseInt(date.substring(4, 8),10);          if(isNaN(month) || isNaN(day) || isNaN(year)) return false;          if((month < 1) || (month > 12)) valid = false;         else if((day < 1) || (day > 31)) valid = false;         else if(((month == 4) || (month == 6) || (month == 9) || (month == 11)) && (day > 30)) valid = false;         else if((month == 2) && (((year % 400) == 0) || ((year % 4) == 0)) && ((year % 100) != 0) && (day > 29)) valid = false;         else if((month == 2) && ((year % 100) == 0) && (day > 29)) valid = false;         else if((month == 2) && (day > 28)) valid = false;      return valid; } 

This checks for valid days in each month and for valid leap year days.

like image 33
Matthew Carroll Avatar answered Sep 18 '22 01:09

Matthew Carroll