Синтаксис даты можно проверить с помощью JavaScript регулярные выражения .Вы можете проверить семантику, используя объект Date , например:
function ValidateCustomDate(d) {
var match = /^(\d{1,2})-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{4})$/.exec(d);
if (!match) {
// pattern matching failed hence the date is syntactically incorrect
return false;
}
var day = parseInt(match[1], 10); // radix (10) is required otherwise you will get unexpected results for 08 and 09
var month = {
Jan: 0,
Feb: 1,
Mar: 2,
Apr: 3,
May: 4,
Jun: 5,
Jul: 6,
Aug: 7,
Sep: 8,
Oct: 9,
Nov: 10,
Dec: 11
}[match[2]]; // there must be a shorter, simpler and cleaner way
var year = parseInt(match[3], 10);
var date = new Date(year, month, day);
// now, Date() will happily accept invalid values and convert them to valid ones
// 31-Apr-2011 becomes 1-May-2011 automatically
// therefore you should compare input day-month-year with generated day-month-year
return date.getDate() == day && date.getMonth() == month && date.getFullYear() == year;
}
console.log(ValidateCustomDate("1-Jan-2011")); // true
console.log(ValidateCustomDate("01-Jan-2011")); // true
console.log(ValidateCustomDate("29-Feb-2011")); // false
console.log(ValidateCustomDate("29-Feb-2012")); // true
console.log(ValidateCustomDate("31-Mar-2011")); // true
console.log(ValidateCustomDate("31-Apr-2011")); // false