Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Validate the user entered date using JavaScript - buggy (UTC) - Gives invalid date for 01/01/1970
function checkDate(str)
{
var matches = str.match(/(\d{1,2})[\/](\d{1,2})[\/](\d{4})/);
if (!matches) return;
var day = parseInt(matches[1],10);
var month = parseInt(matches[2],10);
var year = parseInt(matches[3],10);
var date = new Date(year, month - 1, day);
if (!date || !date.getTime()) return;
if (date.getMonth() + 1 != month ||
date.getFullYear() != year ||
date.getDate() != day) {
return;
}
return(date);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment