信用卡的客户端验证
是否有人有一个库或 JavaScript 片段可以在用户点击“提交”之前验证信用卡的校验位?
Does anyone have a library or JavaScript snippet to validate the check digit of credit cards before the user hits Submit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
jQuery 验证插件 有一个 用于验证信用卡号的方法。
还有其他具体脚本:
大多数都使用Luhn 算法。
The jQuery Validation Plugin has a method for validating credit card numbers.
There are other specific scripts:
Most of them use the Luhn algorithm.
也许OP甚至不再关注这个线程,但这可能对其他人有帮助:
http://jquerycreditcardvalidator.com
它检查卡类型,验证其长度并使用 Luhn 算法检查 mod 10。
Probably OP doesn't even follow this thread anymore but this may be helpful for someone else:
http://jquerycreditcardvalidator.com
It checks the card type, validates its length and checks for mod 10 with Luhn algorithm.
您可以使用此代码段通过 Luhn 算法 验证 16 位卡号:
You can use this snippet to validate 16 digits card numbers with Luhn algorithm:
Luhn 算法(也称为 Luhn 公式)可用于验证各种标识号(例如 < strong>信用卡号、IMEI)。
我省略了对该算法的解释,因为它已经被其他人暴露过,但如果你需要最快的Javascript实现,你可以看到它此处。
简而言之...
请注意,链接的源代码采用 ES6 语言(也称为 JavaScript 2015),但以 ES5 进行转译(请参阅 index.js) 并且它经过了全面的单元测试。
此外,它可以在浏览器和/或node.js中使用。
基准测试和其他实现位于 jsperf 上,以验证其高性能。
现在,如果您只是想使用它,请从链接的存储库中获取代码。
否则通过 bower 安装它...
或者通过 npm ...
免责声明:我是 luhn-alg 包的作者。
Luhn algorithm (also known as Luhn formula) is useful to validate a variety of identification numbers (e.g. credit card numbers, IMEI).
I omit the explanation of the algorithm because it has already been exposed by others but if you need the fastest Javascript implementation, you can see it here.
Put simply ...
Note that linked source is in ES6 language (also known as JavaScript 2015), but is transpiled in ES5 (see index.js) and it is fully unit tested.
Furthermore it is usable both in browsers and/or node.js.
Benchmarks and other implementation are on jsperf to verify its high performances.
Now, if you simply want to use it grab the code from the linked repository.
Otherwise install it via bower ...
Or via npm ...
Disclaimer: I am the author of the
luhn-alg
package.如果您尚未使用 jQuery 插件,则可以使用此功能。 它基于 Luhn 算法,可以容忍空格或破折号,因此应该适用于您需要它的大多数数据输入情况。
http://af-design.com/blog /2010/08/18/验证信用卡号码/
You can use this function if you're not already using the jQuery plugin. It's based on the Luhn algorithm and is tolerant of spaces or dashes so should work for most data entry cases you would need it for.
http://af-design.com/blog/2010/08/18/validating-credit-card-numbers/
Luhn公式是信用卡验证中最流行的算法。 并且不要太害怕“算法”这个词,以至于您正在寻找一个库。 这非常容易理解。 从维基百科的描述来看,该算法可以分为3步:
这是我的工作草稿。
Luhn formula is the most popular algorithm in credit card validation. And don't be so afraid of the word
algorithm
that you're looking for a library. It's incredibly easy to understand. From Wikipedia description, this algorithm can be divide in 3 steps:Here is my working draft.