使用jquery验证月份和年份

validating month and year with jquery

本文关键字:jquery 验证 使用      更新时间:2023-09-26

我有一个函数,它接受月份和年份,并检查它是否在过去。它是有效的,除了当你选择一个两位数的月份,比如11/2013。它认为它是在过去。

我如何获得像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;
    } 
}

按年、按月计算

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;
}