Does anyone have a library or JavaScript snippet to validate the check digit of credit cards before the user hits Submit?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
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 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.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.
You can use this snippet to validate 16 digits card numbers with Luhn algorithm:
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.
The jQuery Validation Plugin has a method for validating credit card numbers.
There are other specific scripts:
Most of them use the Luhn algorithm.