我发现这个简单的功能可以返回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';
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
$('#someTextBox').change(function() {
$('#someOutput').text(creditCardTypeFromNumber($(this).val()));
});
Run Code Online (Sandbox Code Playgroud)
这将输出到某个元素,其中包含id="someOutput"当用户更改元素中的文本时触发的文本框的结果id="someTextBox"。