Is Valid Date
Using regular expressions, we can improve on this function that validates a date string. The function takes an input string and an optional format string. The format string is only 3 characters long and determines the relative positions of the month, day, and year. If the format string is omitted, then it will be "MDY" (month first, then day, then year). There are three different types of dividers that can be used in the date string. These are the slash (/), the period (.) and the dash (-). Years can be either 2 digits (00-49 are assumed to be 21st century and 50-99 are assumed to be 20th century) or 4 digits.
The way the regular expression part of the function works is by using the "remember" capabilities of regular expressions. This assures that the same divider is used in both positions. (In other words, you couldn't do MM/DD-YYYY). The "\1" in the regular expression says "use the same thing you used in parentheses before (which is the check for a slash, period, or dash) and apply the check here".
If the string passes the regular expression (there are 2 checks since the year can be either 2 or 4 digits), then additional JavaScript is performed to check the validity of the date. Instead of doing all the checks for the day of the month being out of range and checking for leap years, a simple approach is taken. A new date object is created in JavaScript. If the numbers are out of range (like February 31), JavaScript will still create an object, it will just be adjusted to be a valid date. So create the date and then check to see if it was adjusted by JavaScript. If it was adjusted, then the original date is not a valid date.
function isValidDate(dateStr, format) {
if (format == null) { format = "MDY"; }
format = format.toUpperCase();
if (format.length != 3) { format = "MDY"; }
if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || _
(format.indexOf("Y") == -1) ) { format = "MDY"; }
if (format.substring(0, 1) == "Y") { // If the year is first
var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
} else if (format.substring(1, 2) == "Y") { // If the year is second
var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
} else { // The year must be third
var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
}
// If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
// Check to see if the 3 parts end up making a valid date
if (format.substring(0, 1) == "M") { var mm = parts[0]; } else _
if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
if (format.substring(0, 1) == "D") { var dd = parts[0]; } else _
if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else _
if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
if (parseFloat(dd) != dt.getDate()) { return false; }
if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
return true;
}
The function returns true if the date is valid and false if it is not. So, your validation could look something like:
if (!isValidDate(myDateString, "DMY")) { alert("The date is not in the correct format."); }