简体   繁体   English

如何使用jquery.validation creditcard2验证信用卡?

[英]how to validate credit cards using jquery.validation creditcard2?

I was hoping someone can point me in the right direction. 我希望有人可以指出正确的方向。 I am looking to use this plugin along with jquery.validation plugin in my forms but from what I see I would need to use a select tag in order for creditcard2 to function correctly. 我希望在我的表单中将此插件与jquery.validation 插件一起使用,但是从我看到的结果来看,为了使creditcard2能够正常运行,我需要使用select标记。

What I currently have is this: 我目前所拥有的是:

 <p class="ccType">
     <label for="card_type" class="error">You must select a credit card from one of the following.</label>
        <br>
     <input type="radio" value="1000" id="card_type_1000" name="card_type" style="outline:none; border:none;" /> 
        <img src="../visa.png" alt="Visa" width="50" height="30" align="top" /> 
        <input type="radio" value="1002" id="card_type_1002" name="card_type" style="outline:none; border:none;" /> 
        <img src="../mastercard.png" alt="Mastercard" width="50" height="30" align="top" /> 
        <input type="radio" value="1006" id="card_type_1006" name="card_type" style="outline:none; border:none;" /> 
        <img src="../discover.png" alt="Discover" width="50" height="30" align="top" /> 
        <input type="radio" value="1004" id="card_type_1004" name="card_type" style="outline:none; border:none;" /> 
        <img src="../amex.png" alt="American Express" width="50" height="30" align="top" />
    </p>

value = 1000 is "Visa" 值= 1000是“签证”

value = 1002 is "MasterCard" 值= 1002是“ MasterCard”

value = 1006 is "Discover" 值= 1006是“发现”

value = 1004 is "AmEx" 值= 1004是“ AmEx”

The plugin site mentions creating a lookup hash. 插件站点提到创建查找哈希。 I don't know how to create one. 我不知道如何创建一个。 Also the documentation that is on the site only shows examples using <select></select> . 此外,该站点上的文档仅显示使用<select></select>示例。 I am using inputs. 我正在使用输入。

How can I get this to work with the set up that I currently have. 如何使它与当前的设置配合使用。

any suggestions? 有什么建议么?

A lookup hash would look like 查找哈希看起来像

var hash = {
    1000: "Visa",
    1002: "MasterCard",
    1004: "AmEx",
    1006: "Discover"
};

Then you could use the following validation function, of course built into your current setup: 然后,您可以使用以下验证功能,这些功能当然已内置到当前设置中:

$("#myform").validate({
    rules: {
        cardnum: { // this should be the id of the card number field
            creditcard2: function(){ return hash[$('.ccType > input:checked').val()]; }
        }
    }       
});

This rather concise code first looks for all the inputs in your .ccType element. 这段简洁的代码首先在.ccType元素中查找所有inputs Then it selects the one that is checked (if any) and gets its value. 然后,它选择一个被检查的(如果有的话)并获取其值。 Then the value is pushed through the hash to change it to a value the plugin likes. 然后,将值推入哈希值以将其更改为插件喜欢的值。 And... let's hope this works. 还有...希望这能奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM