Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I validate a date in this format (yyyy-mm-dd) using jquery?

I am attempting to validate a date in this format: (yyyy-mm-dd). I found this solution but it is in the wrong format for what I need, as in: (mm/dd/yyyy).

Here is the link to that solution: http://jsfiddle.net/ravi1989/EywSP/848/

My code is below:

function isDate(txtDate) {     var currVal = txtDate;     if(currVal == '')         return false;      var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; //Declare Regex     var dtArray = currVal.match(rxDatePattern); // is format OK?      if (dtArray == null)          return false;      //Checks for mm/dd/yyyy format.     dtMonth = dtArray[1];     dtDay= dtArray[3];     dtYear = dtArray[5];              if (dtMonth < 1 || dtMonth > 12)          return false;     else if (dtDay < 1 || dtDay> 31)          return false;     else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31)          return false;     else if (dtMonth == 2)      {         var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));         if (dtDay> 29 || (dtDay ==29 && !isleap))                  return false;     }     return true; } 

What regex pattern can I use for this that will account for invalid dates and leap years?

like image 947
user2648752 Avatar asked Sep 12 '13 08:09

user2648752


People also ask

How do you check if the date is in YYYY-MM-DD 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, we can conclude that the date string is valid and formatted as YYYY-MM-DD .

How check date is valid or not in jQuery?

Simple JavaScript Function to to check if date is valid.getMonth() + 1) == bits[1] && d. getDate() == Number(bits[0])); } //Test it! var currentDate = new Date('31/09/2011'); console. log(isValidDate(currentDate.

How can get current date in dd mm yyyy format in jQuery?

You can do it like that: var d = new Date(); var month = d. getMonth()+1; var day = d. getDate(); var output = d.


1 Answers

I expanded just slightly on the isValidDate function Thorbin posted above (using a regex). We use a regex to check the format (to prevent us from getting another format which would be valid for Date). After this loose check we then actually run it through the Date constructor and return true or false if it is valid within this format. If it is not a valid date we will get false from this function.

function isValidDate(dateString) {    var regEx = /^\d{4}-\d{2}-\d{2}$/;    if(!dateString.match(regEx)) return false;  // Invalid format    var d = new Date(dateString);    var dNum = d.getTime();    if(!dNum && dNum !== 0) return false; // NaN value, Invalid date    return d.toISOString().slice(0,10) === dateString;  }      /* Example Uses */  console.log(isValidDate("0000-00-00"));  // false  console.log(isValidDate("2015-01-40"));  // false  console.log(isValidDate("2016-11-25"));  // true  console.log(isValidDate("1970-01-01"));  // true = epoch  console.log(isValidDate("2016-02-29"));  // true = leap day  console.log(isValidDate("2013-02-29"));  // false = not leap day
like image 105
Goblinlord Avatar answered Sep 25 '22 15:09

Goblinlord