本文介绍了jQuery查找信用卡类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现这个简单的函数可以返回四种最常见的信用卡类型.
I found this simple function to return the 4 most common credit card types.
作为jQuery的新手,当用户在输入字段中输入信用卡号时,我可以使用哪种jQuery插件显示信用卡类型?
Being new to jQuery, what jQuery plugin can I use to display the credit card type as a user types in a credit card number in a input field?
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"
中的文本时触发.
This will output into some element with id="someOutput"
the result of the text box which fires when the user changes the text in the element id="someTextBox"
.
这篇关于jQuery查找信用卡类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!