KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Validating a date in javascript
PRODUCT: 4D | VERSION: 2004 | PLATFORM: Mac & Win
Published On: July 28, 2006

The following javascript function will return true if the date passed is valid. This is useful for a client-side date validation on web forms. The date must be passed in the ISO 8601 format (YYYY-MM-DD).

function validateDate(date) {
  brokenDate = date.split("-");
  if (brokenDate.length == 3) {
    year = brokenDate[0];
    month = brokenDate[1];
    date = brokenDate[2];
     // check date validity
    testDate = new Date(year, month-1, date);
    if ((testDate.getFullYear() == year) &&
      (testDate.getMonth() == month-1) &&
      (testDate.getDate() == date)) {
        return true;
    } else {
      return false;
    }
  }
}