Verification Process

This article outlines procedures and algorithms for Verifying the accuracy and validity of credit card numbers. Most credit card numbers are encoded with a “Check Digit”. A check digit is a digit added to a number (either at the end or the beginning) that validates the authenticity of the number. By running the algorithm, and comparing the check digit you get from the algorithm with the check digit encoded with the credit card number, you can verify that you have correctly read all of the digits and that they make a valid combination. Its useful when a user has keyed in a credit card number (or scanned it) and you want to validate it before sending it our for debit authorization.

Here is a table outlining the major credit cards that you might want to validate.

MASTERCARD: Prefix: 51-55, Length: 16, Check digit algorithm: mod 10
VISA: Prefix: 4, Length: 13, 16, Check digit algorithm: mod 10
AMEX: Prefix: 34, 37, Length: 15, Check digit algorithm: mod 10
Diners Club/Carte Blanche: Prefix: 300-305, 36, 38, Length: 14, Check digit algorithm: mod 10
Discover: Prefix: 6011, Length: 16, Check digit algorithm: mod 10
enRoute: Prefix: 2014, 2149, Length: 15, Check digit algorithm: any
JCB: Prefix: 3, Length: 16, Check digit algorithm: mod 10
JCB: Prefix: 2131, 1800, Length: 15, Check digit algorithm: mod 10

LUHN Formula for Validation of Primary Account Number

The Luhn algorithm or Luhn formula, also known as the “modulus 10″ or “mod 10” algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers and Canadian Social Insurance Numbers. It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent 2,950,048 , filed on January 6, 1954, and granted on August 23, 1960.

The formula verifies a number against its included check digit, which is usually appended to a partial account number to generate the full account number. This account number must pass the following test:

1. Counting from rightmost digit (which is the check digit) and moving left, double the value of every second digit. For any digits that thus become 10 or more, take the two numbers and add them together. For example, 1111 becomes 2121, while 8763 becomes 7733 (from 2×6=12 → 1+2=3 and 2×8=16 → 1+6=7).
2. Add all these digits together. For example, if 1111 becomes 2121, then 2+1+2+1 is 6; and 8763 becomes 7733, so 7+7+3+3 is 20.
3. If the total ends in 0 (put another way, if the total modulus 10 is congruent to 0), then the number is valid according to the Luhn formula; else it is not valid. So, 1111 is not valid (as shown above, it comes out to 6), while 8763 is valid (as shown above, it comes out to 20).

The following steps are required to validate the primary account number:

Step 1: Double the value of alternate digits of the primary account number beginning with the second digit from the right (the first right–hand digit is the check digit.)

Step 2: Add the individual digits comprising the products obtained in Step 1 to each of the unaffected digits in the original number.

Step 3: The total obtained in Step 2 must be a number ending in zero (30, 40, 50, etc.) for the account number to be validated.

For example, to validate the primary account number 49927398716:

Step 1:

        4 9 9 2 7 3 9 8 7 1 6
         x2  x2  x2  x2  x2 
------------------------------
         18   4   6  16   2

Step 2: 4 +(1+8)+ 9 + (4) + 7 + (6) + 9 +(1+6) + 7 + (2) + 6

Step 3: Sum = 70 : Card number is validated

Note: Card is valid because the 70/10 yields no remainder.

Algorithm Implementation

This C# function implements the algorithm described above, returning true if the given array of digits represents a valid Luhn number, and false otherwise.

bool CheckNumber(int[] digits)
 {
   int sum = 0;
   bool alt = false;
   for(int i = digits.Length - 1; i >= 0; i--)
   {
     if(alt)
     {
       digits[i] *= 2;
       if(digits[i] > 9)
       {
         digits[i] -= 9; 
       }
     }
     sum += digits[i];
     alt = !alt;
   }
   return sum % 10 == 0;
 }


:)