gpt4 book ai didi

jQuery 验证器信用卡号 + 类型匹配

转载 作者:行者123 更新时间:2023-11-30 23:46:21 26 4
gpt4 key购买 nike

尝试添加客户 jQuery 验证器方法,该方法主要检查卡类型和信用卡号的有效性。

即。如果类型是MasterCard,如果卡号不是以5开头,则无效;如果类型是Visa,如果卡号不是以4开头,则无效。我公司只收取MC或Visa费用,所以不需要其他费用。

$.validator.addMethod("CCNumber", function(value, element, param) {
if (param == "MasterCard") {
return value.substring(0,1) != 5;
}
else if (param == "Visa") {
return value.substring(0,1) != 4;
}
});

在这里调用它:

cardnumber: {
required: true,
creditcard: true,
minlength: 13,
maxlength: 16,
digits: true,
CCNumber: function(){ return $('#cardtype').val(); }
},

还有消息...

cardnumber: {
required: "Please enter a valid credit card number",
minlength: "Please enter a valid credit card number",
maxlength: "Please enter a valid credit card number",
digits: "Your credit card number cannot contain spaces.",
CCNumber: "WRONG"
},

最后是 HTML:

<select name="cardtype" id="cardtype">
<option value="MasterCard">MasterCard</option>
<option value="Visa">Visa</option>
</select>

我以前从未编写过 jQuery 方法,但这里什么也没发生。我不完全确定我做错了什么,因为我尝试过其他方法,但找不到任何东西。

最佳答案

jQuery Validation plugin已经有其他方法可用于此类事情。

以下是"Additional Methods" file中使用的默认方法,但随后仅针对 Visa 和 Mastercard 进行了编辑。

jQuery.validator.addMethod("creditcardtypes", function(value, element, param) {
if (/[^0-9-]+/.test(value)) {
return false;
}

value = value.replace(/\D/g, "");

var validTypes = 0x0000;

if (param.mastercard)
validTypes |= 0x0001;
if (param.visa)
validTypes |= 0x0002;

if (validTypes & 0x0001 && /^(5[12345])/.test(value)) { //mastercard
return value.length == 16;
}
if (validTypes & 0x0002 && /^(4)/.test(value)) { //visa
return value.length == 16;
}
return false;
}, "Please enter a valid credit card number.");

关于jQuery 验证器信用卡号 + 类型匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13921456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com