Not a Member Yet,
Click here to Register
How do you like the new design?
It's cool, great job
[tally: 70%] 70%
It's okay
[tally: 20%] 20%
Its OK but I liked the old layout better
[tally: 0%] 0%
Please bring the old one back
[tally: 10%] 10%

votes: 10

ID: 60
Viewed: 3458
Added: Jan 17, 2002
Snippet uploaded by: Jon Hanlon
Written By: Jon Hanlon
Demo: Sorry, no demo

User Rated at: 0 Stars
Rate This:

Thank you for your vote. Please wait...

It appears you already voted for this snippet

It appears your vote value was empty

Function checks for valid date of the form dd/mm/yy or dd/mm/yyyy ( or ddmmyy, or d/m/yy etc.) Utilizes the fact that Javascript will internally convert dodgy dates. eg. 2002,4,44 is June 13, 2002 (remembering that Javascript months go from 0 to 11).

function stringToDate(inStr) { // returns a date from input dd/mm/yyyy
if (!inStr) return false;
var reDate = /^s*(d{1,2})D*(d{1,2})D*(d{2,4})s*$/
var matchArray = reDate.exec(inStr);
if (!matchArray) return false;
// to check for mm/dd/yyyy, swap these next two lines
var tDay = parseInt(matchArray[1],10);
var tMonth = parseInt(matchArray[2],10);
var tYear = parseInt(matchArray[3],10);
if (isNaN(tDay + tMonth + tYear)) return false;
if (tYear.toString().length == 3) return false;
if (tYear.toString().length <= 2)
tYear += (tYear < 50) ? 2000 : 1900;

return isValidDate(tYear, tMonth - 1, tDay);

function isValidDate(inY,inM,inD) {
var testDate = new Date(inY, inM, inD);
if (!testDate) return false;
return ( testDate.getDate() == inD
&& testDate.getMonth() == inM
&& testDate.getFullYear() == inY) ? testDate : false;


<input type=text onchange="dateObject=stringToDate(this,value);">

No Comments to show

Please completely fill out the form below if you want to review this snippet. All reviews are subject to validation.

Replying to a Comment...

Adding your comment. Please wait...

Thanks for adding your comment!. After further review it will be added.

There was a problem adding your comment. Please try again.

Please complete all the fields in the form before sending.

© 2002 - 2018 All Rights Reserved. Conditions