1
我需要幫助我的代碼。我需要調用函數leapYear()到我的FieldValidator()函數來確定用戶給出的年份是否爲閏年。Javascript獲得閏年
這裏是我的代碼:
function FieldValidator() {
var err = "";
var valid = false;
var leap = new leapYear(year)
//year
if(document.getElementById('year').value == ""){
valid = true;
err += "Enter year \n";
document.getElementById('year').style.borderColor = "red";
}
else if(document.getElementById('year').value < 1000 || document.getElementById('year').value > 9999){
valid = true;
err += "Invalid Year \n";
document.getElementById('year').style.borderColor = "red";
}
else {
document.getElementById('year').style.borderColor = "green";
}
//month
if(document.getElementById('month').value == ""){
valid = true;
err += "Enter Month \n";
document.getElementById('month').style.borderColor = "red";
}
else if(document.getElementById('month').value < 1 || document.getElementById('month').value > 12){
valid = true;
err += "Invalid Month\n";
document.getElementById('month').style.borderColor = "red";
}
else {
document.getElementById('month').style.borderColor = "green";
}
//day
if(document.getElementById('day').value == ""){
valid = true;
err += "Enter day \n";
document.getElementById('day').style.borderColor = "red";
}
else if (document.getElementById('month').value == 2) {
if(document.getElementById('year').value == leap()) {
if(document.getElementById('day').value > 29) {
valid = true;
err += "invalid leap\n";
document.getElementById('day').style.borderColor = "red";
}
else {
document.getElementById('day').style.borderColor = "green";
}
}
else if(document.getElementById('year').value != leap()) {
if(document.getElementById('day').value > 28) {
valid = true;
err += "invalid \n";
document.getElementById('day').style.borderColor = "red";
}
else {
document.getElementById('day').style.borderColor = "green";
}
}
}
else if (document.getElementById('month').value != 2) {
if(document.getElementById('day').value < 1 || document.getElementById('day').value > 31) {
valid = true;
err += "Invalid day \n";
document.getElementById('day').style.borderColor = "red";
}
else {
document.getElementById('day').style.borderColor = "green";
}
}
else {
document.getElementById('day').style.borderColor = "green";
}
if(valid){
alert(err)
return false;
}
return true;
}
function leapYear(year)
{
return ((document.getElementById('year').value % 4 == 0) && (document.getElementById('year').value % 100 != 0)) || (document.getElementById('year').value % 400 == 0);
}
這是最好通常會問一個最小的,完整的,可覈查的問題,而不是發佈這麼多的代碼是不相關的問題:http://stackoverflow.com/help/mcve –
阻止您調用該方法的錯誤是什麼?是否有錯誤信息或意外結果? –
問題是它沒有得到我的FieldValidator函數上的leapYear函數。 –