[英]how to validate credit cards using jquery.validation creditcard2?
我希望有人可以指出正确的方向。 我希望在我的表单中将此插件与jquery.validation 插件一起使用,但是从我看到的结果来看,为了使creditcard2能够正常运行,我需要使用select标记。
我目前所拥有的是:
<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>
值= 1000是“签证”
值= 1002是“ MasterCard”
值= 1006是“发现”
值= 1004是“ AmEx”
插件站点提到创建查找哈希。 我不知道如何创建一个。 此外,该站点上的文档仅显示使用<select></select>
示例。 我正在使用输入。
如何使它与当前的设置配合使用。
有什么建议么?
查找哈希看起来像
var hash = {
1000: "Visa",
1002: "MasterCard",
1004: "AmEx",
1006: "Discover"
};
然后,您可以使用以下验证功能,这些功能当然已内置到当前设置中:
$("#myform").validate({
rules: {
cardnum: { // this should be the id of the card number field
creditcard2: function(){ return hash[$('.ccType > input:checked').val()]; }
}
}
});
这段简洁的代码首先在.ccType
元素中查找所有inputs
。 然后,它选择一个被检查的(如果有的话)并获取其值。 然后,将值推入哈希值以将其更改为插件喜欢的值。 还有...希望这能奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.