jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jQuery Validator信用卡号码类型匹配大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
尝试添加客户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;   
  }
});

在这里打电话:

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&万事达.

jQuery.validator.addMethod("creditcardtypes",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 Validator信用卡号码类型匹配全部内容,希望文章能够帮你解决jQuery Validator信用卡号码类型匹配所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。