我发现这个简单的函数可以返回 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"
>.
关于Jquery查找信用卡类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5215307/