我正在檢查MM/DD/YYYY。javascript月份和日期和年份檢查
當您輸入0/0/0或00/00/0000時,我的腳本將失敗。我正在嘗試檢查用戶是否已超過21歲,並且必須輸入有效的兩位數月份,兩位數日期和四位數字年份。
有什麼建議嗎?
$("#gate-box").submit(function() {
var day = $("#day").val();
var month = $("#month").val();
var year = $("#year").val();
var age = 21;
var mydate = new Date();
mydate.setFullYear(year, month - 1, day);
var currdate = new Date();
currdate.setFullYear(currdate.getFullYear() - age);
if ((currdate - mydate) < 0) {
$.msg("Sorry, you must be at least " + age + " to enter.");
return false;
}
else if (month > 12) {
$('#month').css({ 'border': '1px solid red' });
$.msg("Please enter a valid month.");
$('#month').focus();
return false;
}
else if (day > 31) {
$('#month').css({ 'border': 'none' });
$('#day').css({ 'border': '1px solid red' });
$.msg("Please enter a valid day.");
$('#day').focus();
return false;
}
else if (month.length === 0 || day.length === 0 || year.length === 0) {
$('#day').css({ 'border': 'none' });
$.msg("Please enter all fields.");
return false;
}
if ((currdate - mydate) > 0) {
$.colorbox.close()
$.setCookie('diageoagecheck', 'verified', { duration: 3 });
}
return false;
});
嘗試驗證**之前的輸入**使用它們來創建日期對象。 – 2010-10-06 20:15:48
如何檢查兩位數月份,兩位數日期和四位數年份。當您輸入0/0/0或00/00/0000時,腳本目前失敗。 – user244394 2010-10-06 20:26:33
檢查它們是否落入(1-31),(1-12),(1900-2100) – 2010-10-06 20:37:21