使用 Jquery 进行信用卡验证

发布于 2024-08-09 19:38:29 字数 56 浏览 1 评论 0原文

我正在尝试使用 jQuery 验证信用卡号,但我不想使用验证插件,是否还有其他插件可以执行此操作?

I'm trying to validate credit card numbers with jQuery but i dont want to use the validation plugin , is there any other plugin for doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扛刀软妹 2024-08-16 19:38:29

http://en.wikipedia.org/wiki/Luhn_algorithm

您可以将下面的内容最小化为在您的代码中占用的空间非常小。

function isCreditCard( CC )
 {                        
      if (CC.length > 19)
           return (false);

      sum = 0; mul = 1; l = CC.length;
      for (i = 0; i < l; i++)
      {
           digit = CC.substring(l-i-1,l-i);
           tproduct = parseInt(digit ,10)*mul;
           if (tproduct >= 10)
                sum += (tproduct % 10) + 1;
           else
                sum += tproduct;
           if (mul == 1)
                mul++;
           else
                mul--;
      }
      if ((sum % 10) == 0)
           return (true);
      else
           return (false);
 }

http://en.wikipedia.org/wiki/Luhn_algorithm

You could minimize this below into a very small footprint in your code.

function isCreditCard( CC )
 {                        
      if (CC.length > 19)
           return (false);

      sum = 0; mul = 1; l = CC.length;
      for (i = 0; i < l; i++)
      {
           digit = CC.substring(l-i-1,l-i);
           tproduct = parseInt(digit ,10)*mul;
           if (tproduct >= 10)
                sum += (tproduct % 10) + 1;
           else
                sum += tproduct;
           if (mul == 1)
                mul++;
           else
                mul--;
      }
      if ((sum % 10) == 0)
           return (true);
      else
           return (false);
 }
雪若未夕 2024-08-16 19:38:29

如果您想确定其卡号是否有效,您需要检查的不仅仅是 Luhn 数字。

Luhn 数字仅用于检查换位错误,并且很容易被诸如 22222222222222222 之类的数字欺骗。

应检查卡号的前六位数字。这些数字称为发行者识别号,可用于确保该号码在可识别的范围内。不幸的是,您将很难找到 IIN 的完整列表,尤其是因为发行人不断添加和删除范围。维基百科有一个非常基本的概述:银行卡号

如果您可以从IIN,然后您也可以尝试验证数字的长度。

除非您有充分的理由,否则使用验证插件之类的东西要容易得多。

If you want to be certain that its a valid card number, you'll need to check much more than the Luhn digit.

The Luhn digit is only intended for checking transposition errors, and can easily be spoofed with numbers such as 22222222222222222

The first six digits of the card number should be checked. These digits are known as the issuer identification number, and can be used to ensure that the number is within a recognised range. Unfortunately you'll struggle to find a comprehensive list of IIN's, not least because issuers are constantly adding and removing ranges. Wikipedia has a very basic overview though: Bank card number

If you can determine the card type from the IIN, you can then have a stab at validating the length of the number also.

Unless you have very good reason, its much easier to use something like a validation plug-in.

來不及說愛妳 2024-08-16 19:38:29

我建议您使用正则表达式...
您可以检查我的存储库我有一个代码片段来验证 Visa、Master 卡、Discovery、美国运通卡和安全代码

https://github.com/leojavier/RegexCollection


记住...仅仅因为您限制了位数并不意味着它是正确的。

例如:
- 所有 Visa 卡号均以 4 开头。新卡有 16 位数字。
- 所有 MasterCard 号码均以数字 51 到 55 开头。全部由 16 位数字组成。
等等...

index.html 是一个示例,向您展示如何使用它!如果您有任何疑问,请随时给我写信:)

I recommend you to use Regular expression...
You can check my repository I have a code snippet to validate Visa, Master card, Discovery, american express and the Security code

https://github.com/leojavier/RegexCollection


Remember... just because you limit the amount of digits doesn't mean it will be right.

for instance:
- All Visa card numbers start with a 4. New cards have 16 digits.
- All MasterCard numbers start with the numbers 51 through 55. All have 16 digits.
and so on...

The index.html is a sample, to show you how you can use it! Feel free to write me if you have any questions :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文