Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q1765 How do you check if a date is valid?

irt.org | Knowledge Base | JavaScript | Date | Q1765 [ previous next ]

Q1765 How do you check if a date is valid?

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;
    }
  }
}
--></script> 

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.