Date Validation With Javascript

HOME
FORUM
ARTICLES
TUTORIALS
SCRIPTS
LINKS
NEWS
MENTORS
TOOLS
REGISTER

Sorry our partner site rodsdot is currently down. Please try again later

 


Date Validation

There are many examples of client side validation of date fields. Writing code to validate a date in JavaScript can turn into a large an ugly experience. That is why there are so many examples.  Without a doubt, the fastest and best way to validate a field is by use of JavaScript's regular expression object. The hard part is coming up with a good regular expression to validate a date.

The regular expression in this example has been tweaked and improved by many people over many years.  It will ensure any date entered in the mm-dd-yyyy format is valid.  It is leap year aware.  (It may also be used to validate a date time field.)

You do not need to understand the regular expression to take advantage of the power.  You can use the same technique to validate many types of fields and if you do not want to become a regular expression maven, simply check many of the fine regular expression libraries for the regex you need.


(mm/dd/yyyy)

Remember: We validate form data on the client side for the convenience of the end user, to provide immediate feedback, and to prevent sending bad data to server side scripts. If the information from your form is posted to your server side script (ASP/JSP/PHP/Perl) then you must also validate the information in your server side script for security reasons.  Fortunately, those languages all support regular expressions, so you will be able to use the same method to reduce your work.

For dd/mm/yyyy see: This Page


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="Roderick Divilbiss">
<meta name="copyright" content="? 2005, 2006 Roderick Divilbiss">
<title>Date Validation</title>
<script type="text/javascript">
<!--
function validateDate(fld) {
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    var errorMessage = 'Please enter valid date as month, day, and four digit year.\nYou may use a slash, hyphen or period to separate the values.\nThe date must be a real date. 2-30-2000 would not be accepted.\nFormay mm/dd/yyyy.';
    if ((fld.value.match(RegExPattern)) && (fld.value!='')) {
        alert('Date is OK'); 
    } else {
        alert(errorMessage);
        fld.focus();
    } 
}
//-->
</script>
</head>

<body>
<form action="dateValidate.htm" method="post">
<p><input type="text" name="date" onblur="validateDate(this);">
<input type="button" value="Click Me">
<br>(mm/dd/yyyy)
</form>
</body>

</html>

A picture of Roderick (Rod) Divilbiss taken June 2005. Visit Rod at www.rodsdot.com.
Creative Commons License© 2006 Rod Divilbiss.
Except where otherwise noted, this content is licensed under a Creative Commons License Creative Commons License.
Content provided exclusively for Experts Round Table by Rod Divilbiss web developer and restaurateur.
Visit Rod at <!--rodsdot.com--> or in person at Cafe Song
e-mail author
Page viewed 70562 times.

post to Dzone Digg this! Add to del.icio.us Googleize this Add to reddit Save to myYahoo Add to furl Add to Netvouz! Spurl this! Add to Linkroll! Save to Simpy Give if thumbs up on StumbleUpon Save to Blinklist Add to Tektag Save to Bibsonomy Submit to Tweako
Search ERT on the Tools Page
Did you know? You can discuss this article with the mentor who wrote it and others interested in the topic? You are invited to join the discussion with Go to the forum

Got a technical article or tutorial you want to publish on the Internet? Join Go to the forum in the Round Table Forum and let the Mentors know what you have. If it meets ERT standards, is factual and can help ERT visitors, then ERT Mentors and Editors can help you (without charge) polish your offering so it can be published and promoted by ERT. An article published on ERT may be read by as many as 10,000 visitors a week; promoting you, your site, and your ideas. Please note ERT does not publish re-prints; promotional handouts, or pieces consisting mainly of links. So original technical content only please. If you prefer you can email the Editor