Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of Luhn algorithm

I am trying to implement simple validation of credit card numbers. I read about the Luhn algorithm on Wikipedia:

  1. Counting from the check digit, which is the rightmost, and moving left, double the value of every second digit.
  2. Sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5) together with the undoubled digits from the original number.
  3. 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.

On Wikipedia, the description of the Luhn algorithm is very easily understood. However, I have also seen other implementations of the Luhn algorithm on Rosetta Code and elsewhere (archived).

Those implementations work very well, but I am confused about why they can use an array to do the work. The array they use seems to have no relation with Luhn algorithm, and I can't see how they achieve the steps described on Wikipedia.

Why are they using arrays? What is the significance of them, and how are they used to implement the algorithm as described by Wikipedia?

like image 570
Mithril Avatar asked Sep 07 '12 02:09

Mithril


People also ask

What is Luhn algorithm for credit card validation?

Luhn's algorithm determines whether or not a credit card number is valid. For a given credit card number: Double the value of every other digit from right to left, beginning with the second to last digit. Add the digits of the results of Step 1 to the remaining digits in the credit card number.

Who created Luhn algorithm?

Luhn Algorithm, or Modulus 10 Algorithm, is a mathematical formula that helps to determine whether or not a correct identification number has been provided. It is named after its creator, German Computer Scientist Hans Peter Luhn, who developed the Luhn Algorithm formula in 1954 during his days as an IBM researcher.

What algorithm do credit cards use?

The final digits of your credit card number is a check digit, akin to a checksum. The algorithm used to arrive at the proper check digit is called the Luhn algorithm, after IBM scientist Hans Peter Luhn (1896-1964). The LUHN algorithm, also known as a Mod 10 calculation, can be used to validate primary account numbers.


1 Answers

Unfortunately none of the codes above worked for me. But I found on GitHub a working solution

// takes the form field value and returns true on valid number
function valid_credit_card(value) {
// accept only digits, dashes or spaces
    if (/[^0-9-\s]+/.test(value)) return false;

// The Luhn Algorithm. It's so pretty.
    var nCheck = 0, nDigit = 0, bEven = false;
    value = value.replace(/\D/g, "");

    for (var n = value.length - 1; n >= 0; n--) {
        var cDigit = value.charAt(n),
            nDigit = parseInt(cDigit, 10);

        if (bEven) {
            if ((nDigit *= 2) > 9) nDigit -= 9;
        }

        nCheck += nDigit;
        bEven = !bEven;
    }

    return (nCheck % 10) == 0;
}
like image 103
Ron Avatar answered Oct 16 '22 11:10

Ron