Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap

Q1765 How do you check if a date is valid?

You are here: irt.org | FAQ | JavaScript | Date | Q1765 [ previous next ]

This is the most simple way to do a JavaScript date validation. I usually call this onBlur, because I only want to check it if the user fills it in. If it's blank, I just pass it up.

// this section goes in your form:
//<input type="text" onBlur="return checkDate(this.value);">

//Put the following in <HEAD> section:
<script language="JavaScript"><!--
function checkDate(theDate) {
  // If the value is not blank, validate the date.
  // using isNaN (is not a number) and date parse.
  // If the date is invalid, give the user an alert
  // message, set the form focus to the date field.
  // and return false. If it's valid then return true.

  if (theDate!= "") {
    if (isNaN(Date.parse(theDate))) {
      alert("Please enter yourDate in mm/dd/yy format.");
      return false;
    } else {
      return true;

Feedback on 'Q1765 How do you check if a date is valid?'

©2018 Martin Webb