以下是驗證日期的函數。應該在今天 - 15和今天。有人可以重構此代碼。JavaScript驗證日期
phpdatetoday爲以下形式的字符串2010年,月,3
function validate(page, phpdatetoday)
{
var i = 0;
var fields = new Array();
var fieldname = new Array();
var day = document.getElementById('date_of_inspection_day').value;
var month = document.getElementById('date_of_inspection_month').value;
var year = document.getElementById('date_of_inspection_year').value;
var datesubmitted = new Date(year,month-1,day);
var daysInMonth = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
if(month.length<1)
{
alert("Please enter a valid month");
return false;
}
if(year.length != 4)
{
alert("Please enter a valid year");
return false;
}
if (day.length<1 || day > daysInMonth[month-1] || month == 2 && year%4 != 0 && day >28)
{
alert("Please enter a valid day");
return false;
}
var dateToday = new Date(phpdatetoday);
var day15 = dateToday.getDate()-15; // 15 days old
var month15 = dateToday.getMonth();
var year15 = dateToday.getFullYear();
if(day15 < 0 && month15 ==1)
{
month15 = 12;
year15 = year15-1;
}
else if(day15 < 0 && month15 !=1)
{
month15 = month15-1;
}
day15 = daysInMonth[month15-1] + day15;
var date15DayOld = new Date(year15,month15,day15);
if(date15DayOld > datesubmitted)
{
alert("Your date is older than 15 days");
}
else if(datetoday < datesubmitted)
{
alert("invalid Date");
}
}
如果此代碼有效,請保持原樣。如果有問題,請詢問具體問題。你爲什麼要重構這個? – Kamarey 2010-12-03 09:18:15