Jquery找到信用卡类型

我发现这个简单的function可以返回4种最常见的信用卡类型。

作为jQuery的新手,我可以使用什么jQuery插件来显示信用卡类型,因为用户在输入字段中输入信用卡号?

function creditCardTypeFromNumber(num) { // first, sanitize the number by removing all non-digit characters. num = num.replace(/[^\d]/g,''); // now test the number against some regexes to figure out the card type. if (num.match(/^5[1-5]\d{14}$/)) { return 'MasterCard'; } else if (num.match(/^4\d{15}/) || num.match(/^4\d{12}/)) { return 'Visa'; } else if (num.match(/^3[47]\d{13}/)) { return 'AmEx'; } else if (num.match(/^6011\d{12}/)) { return 'Discover'; } return 'UNKNOWN'; } 

谢谢!

 $('#someTextBox').change(function() { $('#someOutput').text(creditCardTypeFromNumber($(this).val())); }); 

这将输出到id="someOutput"的某个元素,该文本框的结果在用户更改元素id="someTextBox"的文本时触发。

http://www.ihwy.com/labs/jquery-validate-credit-card-extension.aspx

http://docs.jquery.com/Plugins/Validation/Methods/creditcard

默认情况下,插件可能无法在您键入时进行卡validation。 要进行实时validation,可以在输入字段上绑定一个简单的keyup() ,以便在每次击键后进行validation。

jQuery信用卡类型检测器插件https://github.com/christianreed/Credit-Card-Type-Detector

此外,仅供参考, 此线程(链接)的答案有一些其他卡类型的正则表达式,以及信用卡号码的一个很棒的解释。

这是一个仅在输入至少6位数字后才触发的function(用于识别卡片类型的function):

 $('#CardNumber').keyup(function(){ if($(this).val().length >= 6){ cardType = creditCardTypeFromNumber($(this).val()); } }); 

使用免费的BIN API服务,例如https://tripayments.com/bins/ 。 提交前6个,它将提供“DetailCardProduct”,即VISA,Mastercard等