Parsley 中有一种方法可以扩展 .js 来检查错误日期。例如:月/日/年
我想将其更改为仅检查月份和年份。例如:MM/YYYY
americandate: function ( val, elem, self) {
if(!/^([01]?[0-9])[\.\/-]([0-3]?[0-9])[\.\/-]([0-9]{4}|[0-9]{2})$/.test(val)) {
return false;
}
var parts = val.split(/[.\/-]+/);
var day = parseInt(parts[1], 10);
var month = parseInt(parts[0], 10);
var year = parseInt(parts[2], 10);
if(year == 0 || month == 0 || month > 12) {
return false;
}
var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
monthLength[1] = 29;
}
return day > 0 && day <= monthLength[month - 1];
}