用jquery验证月份和年份

时间:2013-04-29 20:49:17

标签: javascript jquery validation date

我有一个功能,需要月份和年份,并检查它是否是过去。它正在工作,除非您选择两位数的月份,例如2013年11月。它认为它已经过去了。

如何获取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;
    } 
}

1 个答案:

答案 0 :(得分: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;
}