Jquery查找信用卡类型

标签 jquery

我发现这个简单的函数可以返回 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/

相关文章:

jquery - 使用 jQuery 插件复制表单字段

javascript - 如何通过jquery遍历xml?

c# - 将焦点设置到文本框

c# - javascript/jquery 模态弹出对话框 MVC 4/渲染部分 View

jquery - 检测移动设备和重定向的最佳方法

jquery - 在 jQuery 嵌套 .children() 中选择

javascript - 如何高效使用Jquery Chosen Plugin?

javascript - 在 jQuery 中提取 ajax 响应?

javascript - 自动刷新输入时间对象,无需重新加载页面

javascript - 滚动后滚动到下一个 anchor