Client-side validation of credit cards

风流意气都作罢 提交于 2020-01-11 00:19:33

问题


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


回答1:


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

There are other specific scripts:

  • JavaScript Credit Card Validation Function
  • Credit Card Validation

Most of them use the Luhn algorithm.




回答2:


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.




回答3:


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;
}



回答4:


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.




回答5:


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/




回答6:


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


来源:https://stackoverflow.com/questions/255445/client-side-validation-of-credit-cards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!