HTML中的信用卡失效日期验证 [英] Credit card expiry date validation in HTML

查看:84
本文介绍了HTML中的信用卡失效日期验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML code:

HTML code:

<tr>
    <td><label for="expiry_day">Expiry date(MM/YYYY):<span id="imp">*</span></label></td>
    <td>
    <select id="expiry_month" tabindex="4">
    <optgroup label="Month">
    <option value="01">January</option>
    <option value="02">February</option>
    <option value="03">March</option>
    <option value="04">April</option>
    <option value="05">May</option>
    <option value="06">June</option>
    <option value="07">July</option>
    <option value="08">August</option>
    <option value="09">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
    </optgroup>
    </select>

    <select id="expiry_year" tabindex="5">
    <optgroup label="Year">
    <script>generate_year();</script>   
    </optgroup>
    </select>
    </td>
</tr>

JavaScript代码:

JavaScript code:

function generate_year()                    /*For generate cc year*/
    {
        for (var i = 2014; i <= 2104; i++)

        {
            document.write ("<option value='" + i + "'>" + i + "</option>");
        }   
    }

        function val_cc () {          

        var expiry_month = document.getElementById("expiry_month").value;
        var expiry_year = document.getElementById("expiry_year").value;
        var today = new Date();
        var expiry_date = today.setFullYear(expiry_year, expiry_month);

        if (today.getTime() > expiry_date.getTime())

        {
            alert ("\u2022Expiry month and year cannot be blank/Expiry month and year is before today month and year.");

            return false;
        }
}

这些代码基本上用于验证过期日期例如,如果今天是2014年3月,用户选择2014年2月,我希望表单返回false。但是,如果他选择2014年4月,我希望表单返回true并进入下一页。如果有是错误,你可以请他们指出并做一个简单的解释吗?

These codes are basically used to validate the expiry date for credit card.For example,if today is March 2014 and the user chooses February 2014,I want the form to return false.However,if he chooses April 2014,I want the form to return true and proceed to the next page.If there are mistakes,can you please point them out and do a simple explanation?

推荐答案

您应该每次更新选择框的值用户更改值,请参阅此小提琴 http://jsfiddle.net/shershen08/wcFC4/



You should update values from select-boxes every time user changes values, please see this fiddle http://jsfiddle.net/shershen08/wcFC4/

function runMyCheck(){
//update value every run
var expiry_month = document.getElementById("expiry_month").value;
var expiry_year = document.getElementById("expiry_year").value;

var today = new Date();
var selDate = new Date();

if (today.getTime() > selDate.setFullYear(expiry_year, expiry_month)){
 //too late
    alert ("\u2022Expiry month and year cannot be blank/Expiry month and year is before today month and year.");
    return false;
} else {
//do good stuff...

}

}

这篇关于HTML中的信用卡失效日期验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆