2013-04-29 95 views
0

我有一個函數,它需要月份和年份並檢查它是否在過去。除了當你選擇兩位數月份時,它正在工作,如11/2013。它認爲這是過去。使用jquery驗證月份和年份

如何獲取像10/2013這樣的日期進行驗證?

creditDate = function(){ 
now = new Date(); 
month = now.getUTCMonth(); 
year = now.getUTCFullYear(); 
current = month + "/" + year; 
selectedMonth = $('#ctl00_MainContentHolder_Payment1_CreditCardInput1_ExpMonthField') 
yourMonth = selectedMonth.val(); 
selectedYear = $('#ctl00_MainContentHolder_Payment1_CreditCardInput1_ExpYearField') 
yourYear = selectedYear.val(); 
yourDate = yourMonth + "/" + yourYear 
if (yourDate < current){ 
    selectedMonth.css('background-color', 'pink'); 
    selectedYear.css('background-color', 'pink'); 
    $('#errorAlertTwo').append('Credit card expiration date is invalid.' + '<br>'); 
    return false; 
     } else { 
     return true; 
    } 
} 

回答

0

只要做到這一點的年,月基礎

if (yourYear < currentYear || (yourYear == currentYear && yourMonth < currentMonth)) { 
    selectedMonth.css('background-color', 'pink'); 
    selectedYear.css('background-color', 'pink'); 
    $('#errorAlertTwo').append('Credit card expiration date is invalid.' + '<br>'); 
    return false; 
    } 
else { 
    return true; 
}