I added a "truedate" method to the validator. I'd love to credit whoever's code this is, but I don't remember where I found it.
$.validator.addMethod("truedate", function (value) {
function GetFullYear(year) {
var twoDigitCutoffYear = 10 % 100;
var cutoffYearCentury = 10 - twoDigitCutoffYear;
return ((year > twoDigitCutoffYear) ? (cutoffYearCentury - 100 + year) : (cutoffYearCentury + year));
}
if (value == null || value == '')
return true;
var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\.?\\s*$");
try {
m = value.match(yearFirstExp);
var day, month, year;
if (m != null && (m[2].length == 4)) {
day = m[6];
month = m[5];
year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3]));
}
else {
var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})(?:\\s|\\2)((\\d{4})|(\\d{2}))(?:\\s\u0433\\.)?\\s*$");
m = value.match(yearLastExp);
if (m == null) {
return null;
}
day = m[3];
month = m[1];
year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6]));
}
month -= 1;
var date = new Date(year, month, day);
if (year < 100) {
date.setFullYear(year);
}
return (typeof (date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
}
catch (err) {
return null;
}
}, "Please enter an actual date.");
$('form').validate({
rules: {
insurance_GL: "truedate",
},
messages: {
insurance_GL: "Insurance GL date is not valid",
}
});
EDIT
I must have gotten it from here:
http://stackoverflow.com/questions/2715626/jquery-datepicker-validate-date-mm-dd-yyyy