jsq*_*lla 5 jquery jquery-validate
尝试添加客户jQuery验证器方法,该方法基本上检查卡类型和信用卡号的有效性.
即.如果类型为万事达卡,如果卡号不以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;
}
});
Run Code Online (Sandbox Code Playgroud)
在这里打电话:
cardnumber: {
required: true,
creditcard: true,
minlength: 13,
maxlength: 16,
digits: true,
CCNumber: function(){ return $('#cardtype').val(); }
},
Run Code Online (Sandbox Code Playgroud)
而且信息......
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"
},
Run Code Online (Sandbox Code Playgroud)
最后,HTML:
<select name="cardtype" id="cardtype">
<option value="MasterCard">MasterCard</option>
<option value="Visa">Visa</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我之前从未编写过jQuery方法,但这里什么也没发生.我不完全确定我做错了什么,因为我试图查看其他方法,但找不到任何东西.
jQuery 验证插件已经有其他方法可用于此类操作。
以下是“其他方法”文件中使用的默认方法,但随后仅针对 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.");
Run Code Online (Sandbox Code Playgroud)