2015-10-02 101 views
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); 
} 
+0

這是最好通常會問一個最小的,完整的,可覈查的問題,而不是發佈這麼多的代碼是不相關的問題:http://stackoverflow.com/help/mcve –

+0

阻止您調用該方法的錯誤是什麼?是否有錯誤信息或意外結果? –

+0

問題是它沒有得到我的FieldValidator函數上的leapYear函數。 –

回答

3

對於相關的任何時候,我使用momentjs(或者就像你在瀏覽器中或在的NodeJS服務器上)。

就這麼簡單。

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"> 
var year = 2015; 

moment([year]).isLeapYear(); // false 
</script> 

isLeapYear函數本身也退出簡單。因此,您可以使用它來確定年份是否爲閏年。

function isLeapYear(year) { 
    return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; 
} 

Source