Google+

Tuesday, July 19, 2011

Credit Card Number Validation

Informal explanation

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 the check digit, which is the rightmost, and moving left, double the value of every second digit.
  2. Sum the digits of the products (eg, 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.
Assume an example of an account number "4992739871" that will have a check digit added, making it of the form 4992739871x:
Account number 4 9 9 2 7 3 9 8 7 1 x
Double every other 4 18 9 4 7 6 9 16 7 2 x
Sum together all numbers 64 + x

To make the sum divisible by 10, we set the check digit (x) to 6, making the full account number 49927398716.
The account number 49927398716 can be validated as follows:
  1. Double every second digit, from the rightmost: (1×2) = 2, (8×2) = 16, (3×2) = 6, (2×2) = 4, (9×2) = 18
  2. Sum all the individual digits (digits in parentheses are the products from Step 1): 6 + (2) + 7 + (1+6) + 9 + (6) + 7 + (4) + 9 + (1+8) + 4 = 70
  3. Take the sum modulo 10: 70 mod 10 = 0; the account number is probably valid.


 Sample Code to Check Card No.


public boolean luhnCardValidationMethod(String cardNumber) {
        char[] charArray = cardNumber.toCharArray();
        int[] number = new int[charArray.length];
        int total = 0;
        for (int i = 0; i < charArray.length; i++)
        {
            number[i] = Character.getNumericValue(charArray[i]);
        }

        for (int i = number.length - 2; i > -1; i -= 2)
        {
            number[i] *= 2;

            if (number[i] > 9)
                number[i] -= 9;
        }

        for (int i = 0; i < number.length; i++)
        {
            total += number[i];
        }

        if (total % 10 != 0)
        {
            //TODO if total%10 is not Equals to Zero the Card is Invalid
            return false;
        }

        //TODO Else Card is Valid
        return true;
    }

Source : http://en.wikipedia.org/wiki/Luhn_algorithm



Some More Tips to Check Card Number.

1. Prefix, Length, and Check Digit Criteria

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

CARD TYPE Prefix Length Check digit algorithm
MASTERCARD51-5516 mod 10
VISA413, 16 mod 10
AMEX34
37
15 mod 10
Diners Club/
Carte Blanche
300-305
36
38
14mod 10
Discover601116 mod 10
enRoute2014
2149
15any
JCB316 mod 10
JCB2131
1800
15 mod 10

2. LUHN Formula (Mod 10) for Validation of Primary Account Number

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.


Source : http://www.beachnet.com/~hstiles/cardtype.html


Comments Always Welcome..... :)

    No comments:

    Google+