js date validation

this is a simple date validation that uses regular expression as a
validator.Otherwise we'll end up writing a long math function to
validate a date.

this code only validates mm/dd/yyyy format but it should be easy to be modified to support other formats.
function isDate(sDate) {
   var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
   if (re.test(sDate)) {
      var dArr = sDate.split("/");
      var d = new Date(sDate);
      return d.getMonth() + 1 == dArr[0] && d.getDate() == dArr[1] && d.getFullYear() == dArr[2];
   }
   else {
      return false;
   }
}

reference : codingforums.com

0 comments:

Post a Comment