Client-side validation of credit cards

2020-03-01 09:48发布

Does anyone have a library or JavaScript snippet to validate the check digit of credit cards before the user hits Submit?

6条回答
唯我独甜
2楼-- · 2020-03-01 09:50

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/

查看更多
倾城 Initia
3楼-- · 2020-03-01 09:53

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 ...

function luhn(array) {
  return function (number) {
    let len = number ? number.length : 0,
      bit = 1,
      sum = 0;

    while (len--) {
      sum += !(bit ^= 1) ? parseInt(number[len], 10) : array[number[len]];
    }
    return sum % 10 === 0 && sum > 0;
  };
}([0, 2, 4, 6, 8, 1, 3, 5, 7, 9]);

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 ...

bower install luhn-alg

Or via npm ...

npm install luhn-alg

Disclaimer: I am the author of the luhn-alg package.

查看更多
Juvenile、少年°
4楼-- · 2020-03-01 10:01

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.

查看更多
姐就是有狂的资本
5楼-- · 2020-03-01 10:03

You can use this snippet to validate 16 digits card numbers with Luhn algorithm:

function validateCardNumber(number) {
    var regex = new RegExp("^[0-9]{16}$");
    if (!regex.test(number))
        return false;

    return luhnCheck(number);
}

function luhnCheck(val) {
    var sum = 0;
    for (var i = 0; i < val.length; i++) {
        var intVal = parseInt(val.substr(i, 1));
        if (i % 2 == 0) {
            intVal *= 2;
            if (intVal > 9) {
                intVal = 1 + (intVal % 10);
            }
        }
        sum += intVal;
    }
    return (sum % 10) == 0;
}
查看更多
趁早两清
6楼-- · 2020-03-01 10:05

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:

  • From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if the product of this doubling operation is greater than 9 (e.g., 8 × 2 = 16), then sum the digits of the products (e.g., 16: 1 + 6 = 7, 18: 1 + 8 = 9).
  • Take the sum of all the digits.
  • If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not valid.

Here is my working draft.

function luhn(anum){
    anum = anum+'';
    var sum = 0,
        max = anum.length - 1;
    //From the rightmost digit, which is the check digit, moving left
    for(var j=max;j>=0;j--){
        var digit = parseInt(anum[j]);
        //Take the sum of all the digits
        if((max - j) & 1){
            //double the value of every second digit
            var add = digit * 2;
            //if the product of this doubling operation is greater than 9 ,
            //then sum the digits of the products
            sum += add < 10 ? add : 1 + add % 10;
        }else{
            sum += digit;
        }
    }
    //If the total modulo 10 is equal to 0 (if the total ends in zero)
    //then the number is valid according to the Luhn formula;else it is not valid.
    return sum % 10 === 0;
}

luhn(79927398713) -> true
查看更多
对你真心纯属浪费
7楼-- · 2020-03-01 10:07

The jQuery Validation Plugin has a method for validating credit card numbers.

There are other specific scripts:

Most of them use the Luhn algorithm.

查看更多
登录 后发表回答