Created
October 4, 2017 14:22
-
-
Save philippwiddra/ae18303d64bd732c544e27a2e6fc9856 to your computer and use it in GitHub Desktop.
Extension for jquery.validate to be used with ASP.Net MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.validator.methods.date = function (value, element) { | |
var extractGermanDate = function (dateString) { | |
var matches = /^(\d{1,2})\.(\d{1,2})\.(\d{4})( (\d{1,2}):(\d{1,2})(:(\d{1,2}))?)?$/.exec(dateString); | |
if (matches == null) { | |
return false; | |
} else { | |
return { | |
day: matches[1], | |
month: matches[2] - 1, | |
year: matches[3], | |
hour: matches[5] || 0, | |
minute: matches[6] || 0, | |
second: matches[8] || 0 | |
}; | |
} | |
}; | |
var extractIsoDate = function (dateString) { | |
var matches = /^(\d{4})-(\d{2})-(\d{2})( (\d{2}):(\d{2})(:(\d{2}))?)?$/.exec(dateString); | |
if (matches == null) { | |
return false; | |
} else { | |
return { | |
day: matches[3], | |
month: matches[2] - 1, | |
year: matches[1], | |
hour: matches[5] || 0, | |
minute: matches[6] || 0, | |
second: matches[8] || 0 | |
}; | |
} | |
}; | |
var validateDate = function (dateString) { | |
var dateData = extractIsoDate(dateString) || | |
extractGermanDate(dateString); | |
if (!dateData) return false; | |
var date = new Date(dateData.year, dateData.month, dateData.day, dateData.hour, dateData.minute, dateData.second); | |
return !/Invalid|NaN/.test(date.toString()) && | |
date.getDate() == dateData.day && | |
date.getMonth() == dateData.month && | |
date.getFullYear() == dateData.year && | |
date.getHours() == dateData.hour && | |
date.getMinutes() == dateData.minute && | |
date.getSeconds() == dateData.second; | |
}; | |
return this.optional(element) || validateDate(value); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment