月份和年份的到期日

时间:2011-05-03 22:29:10

标签: javascript date

在尝试解决这个问题时遇到了一些麻烦,当涉及到JavaScript时,请完成新手。我正在尝试验证表格,更具体地说是信用卡的到期日。 例如2011年1月将无效,但2011年12月将是有效的等。 这就是我到目前为止所做的:

function ExpiryDate() {

var year
var months

today = new Date();
    expiry = new Date(year, month);
    if (today.getTime() > expiry.getTime())

    return false;
else
    return true;
};

我的表格上写着:

<form id="myform" method="post" action="default.html" onsubmit="return Validate(this);">

<p>Expiration Date: Month

<select name="ExpMon" id="ExpMon" title="select a month"> 
        <option value="1">Jan</option> 
        <option value="2">Feb</option> 
        <option value="3">Mar</option>
        <option value="4">Apr</option> 
        <option value="5">May</option> 
        <option value="6">June</option> 
        <option value="7">July</option> 
        <option value="8">Aug</option> 
        <option value="9">Sept</option> 
        <option value="10">Oct</option> 
        <option value="11">Nov</option> 
        <option value="12">Dec</option> 
 </select>      

 Year:
 <select name="ExpYear" id="ExpYear" title="select a year"> 
     <option value="2010">2010</option> 
     <option value="2011">2011</option> 
     <option value="2012">2012</option> 
     <option value="2013">2013</option>
     <option value="2014">2014</option> 
     <option value="2015">2015</option>
</select></p> 

</form

我不确定如何使用id来获得我想要的东西,任何帮助都会受到赞赏,在此先感谢。注意:我有一个名为Validate的函数,它验证了我的表单中的文本字段,因此我可以以某种方式将其附加到此。

5 个答案:

答案 0 :(得分:1)

var year = document.getElementById("ExpYear").value;
var month = document.getElementById("ExpMon").value;

如果您愿意,可以改用名字;你必须得到Validate才能将表单元素作为参数传递,但是你可以使用theForm.ExpYear.valuetheForm.ExpMonth.value

注意:当您只需撰写if (today.getTime() > expiry.getTime()) return false; else return true;时,很多人不喜欢return today.getTime() <= expiry.getTime();形式的陈述。

答案 1 :(得分:1)

Neil已回答了有关如何获取值(+1)的问题。我只是想补充一点,恕我直言,这将是一个更好的用户体验,不允许你的用户首先选择一个不可接受的到期日。

这是一个更多的努力,但基本上,通过响应所选月份,您可以查看当前时间,然后根据该月重置允许年份。同样,根据所选年份,允许月份的集合将根据当前时间而变化。

不要让用户犯错,然后发出警告,不要让错误完全发生。

答案 2 :(得分:1)

以下将获取用户输入,创建日期对象,然后将其与今天的日期进行比较。一个关键项目是它将找到当月的最后一天,以便即使今天是5月3日并且用户选择“2011年5月”(对信用卡交易有效),它也将返回“有效”。 / p>

function CheckDate() {
    var selectedDate = new Date (document.getElementById("ExpYear").value,document.getElementById("ExpMon").value)
    var nextmonth = selectedDate.setMonth(selectedDate.getMonth() + 1);
    var last_date_of_selected_date = new Date(nextmonth -1);
    var today = new Date();
    if (today > selectedDate) {
        alert("Invalid");
    }
    else {
        alert("Valid");
    }
}

这是一个有效的jsFiddle,可以看到这个:http://jsfiddle.net/cPApB/3/

答案 3 :(得分:0)

试试这个:

var today = new Date();
var expDate = new Date($("#cart-expyear").val(),($("#cart-expmonth").val()-1)); // JS Date Month is 0-11 not 1-12 replace parameters with year and month
if(today.getTime() > expDate.getTime()) {
    console.log("Your Card is expired. Please check expiry date.");
}

答案 4 :(得分:-2)

使用此简单验证

var expiryYear = $('#<%# DataBinder.Eval (ddlYear,"ClientID") %> option:selected').val();
var expiryMonth = $('#<%# DataBinder.Eval (ddlValidMonths,"ClientID") %> option:selected').val();
            var currentUserDate = $('#<%# DataBinder.Eval (hdnFieldJsonDate,"ClientID") %>').val();
            if (currentUserDate != null && expiryMonth != null && expiryYear != null) {
                currentUserDate = JSON.parse(currentUserDate);
                if (expiryMonth > 0 && expiryYear > 0) {

                    //If the Expiry Year smaller than Current Year, then validation failed
                    if (expiryYear < currentUserDate.Year)
                    { args.IsValid = false; }
                    //If the Expiry Year larger than Current Year, then validation passed
                    else if (expiryYear > currentUserDate.Year)
                    { args.IsValid = true; }
                    //If the Expiry Year is same as Current Year, and if Expiry Month is smaller than current month, then validation failes, else passed.
                    else if (expiryYear == currentUserDate.Year) {
                        if (expiryMonth < currentUserDate.Month)
                            args.IsValid = false;
                        else
                            args.IsValid = true;
                    }
                }
                else
                { args.IsValid = true; }
            }