js校驗日期的合法性?

//校驗日期的格式是否合法.格式為:2007-10-22

function checkDate(checkString, dateLength, elementName, msgobj, type) {

if (type == "0") {

var flag;

if (checkString.value.length != dateLength) {

msgobj.innerHTML = elementName + " 必須等於 : " + dateLength+" 位";

checkString.select();

checkString.focus();

return false;

}

if (!checkNumOrSidelong(checkString, elementName, msgobj, type)) {

msgobj.innerHTML = elementName + " 的日期格式不合法";

checkString.select();

checkString.focus();

return false;

}

var year = checkString.value.substring(0, 4);

var month = checkString.value.substring(5, 7);

var day = checkString.value.substr(8, 10);

if(!(dataIsNumber(year,true)&&dataIsNumber(month,true)&&dataIsNumber(day,true))){

msgobj.innerHTML = elementName + " 的日期格式不合法";

checkString.select();

checkString.focus();

return false;

}

if(!(isHyphen(checkString.value.substring(4,5))&&isHyphen(checkString.value.substring(7,8)))){

msgobj.innerHTML = elementName + " 的日期格式不合法";

checkString.select();

checkString.focus();

return false;

}

if ((month == 4 month == 6 month == 9 month == 11) && (day > 30)) { // 4,6,9,11月份日期不能超過30

msgobj.innerHTML = elementName + " 在當前月份不能超過 30 天!";

checkString.select();

checkString.focus();

return false;

}

if (month == 2) {

if (LeapYear(year)) {

if (day > 29 day < 1) {

msgobj.innerHTML = elementName + " 為潤年,因此2月份的天數必須在01-29之間!";

checkString.select();

checkString.focus();

return false;

}

} else {

if (day > 28 day < 1) {

msgobj.innerHTML = elementName + " 並非潤年,因此2月份的天數必須在01-28之間";

checkString.select();

checkString.focus();

return false;

}

}

}

if (year < 1900 year > 2050) {

msgobj.innerHTML = elementName + "年份必須是在:1900-2050之間";

checkString.select();

checkString.focus();

return false;

}

if (month < 1 month > 12) {

msgobj.innerHTML = elementName + "月份必須是在:01-12之間";

checkString.select();

checkString.focus();

return false;

}

if (day < 1 day > 31) {

msgobj.innerHTML = elementName + "天數必須是在:01-31之間";

checkString.focus();

return false;

}

} else {

if (type == "1") {

/*if (checkString.value == "") {

alert("日期不能為空");

checkString.focus();

return false;

}*/

var flag;

if (checkString.value.length != dateLength) {

alert(elementName + "長度必須為: " + dateLength+"位");

checkString.select();

checkString.focus();

return false;

}

if (!checkNumOrSidelong(checkString, elementName, msgobj, type)) {

alert(elementName + " 的日期格式不合法");

checkString.select();

checkString.focus();

return false;

}

var year = checkString.value.substring(0, 4);

var month = checkString.value.substring(5, 7);

var day = checkString.value.substr(8, 10);

if(!(dataIsNumber(year,true)&&dataIsNumber(month,true)&&dataIsNumber(day,true))){

msgobj.innerHTML = elementName + " 的日期格式不合法";

checkString.select();

checkString.focus();

return false;

}

if(!(isHyphen(checkString.value.substring(4,5))&&isHyphen(checkString.value.substring(7,8)))){

msgobj.innerHTML = elementName + " 的日期格式不合法";

checkString.select();

checkString.focus();

return false;

}

if ((month == 4 month == 6 month == 9 month == 11) && (day > 30)) { // 4,6,9,11月份日期不能超過30

alert( elementName + " 在當前月份不能超過 30 天!");

checkString.select();

checkString.focus();

return false;

}

if (month == 2) {

if (LeapYear(year)) {

if (day > 29 day < 1) {

alert( elementName + " 為潤年,因此2月份的天數必須在01-29之間!");

checkString.select();

checkString.focus();

return false;

}

} else {

if (day > 28 day < 1) {

alert( elementName + "並非為潤年,因此2月份的天數必須在01-28之間!");

checkString.select();

checkString.focus();

return false;

}

}

}

if (year < 1900 year > 2050) {

alert(elementName + "年份必須是在:1900-2050之間");

checkString.select();

checkString.focus();

return false;

}

if (month < 1 month > 12) {

alert(elementName + "月份必須是在:01-12之間");

checkString.select();

checkString.focus();

return false;

}

if (day < 1 day > 31) {

alert(elementName + "天數必須是在:01-31之間");

checkString.select();

checkString.focus();

return false;

}

}

}

return true;

}

相關問題答案