Credit Card Validation - Check Digits
This document 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. A simple algorithm
is applied to the other digits of the number which yields the
check digit. 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.
1. Prefix, Length, and Check Digit Criteria
Here is a table outlining the major credit cards that you might
want to validate.
Check digit
CARD TYPE Prefix Length algorithm
---------- ------ ------ -----------
MASTERCARD 51-55 16 mod 10
VISA 4 13, 16 mod 10
AMEX 34, 37 15 mod 10
Diners Club/ 300-305, 14 mod 10
Carte Blanche 36, 38
Discover 6011 16 mod 10
enRoute 2014, 15 any
2149
JCB 3 16 mod 10
JCB 2131, 15 mod 10
1800
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.
Note: remember that when you double a number over 4, (9 for example)
you don't add the result to your total, but rather the sum of
the digits of the result (in the above example 9*2=18 so you would
add 1+8 to your total (not 18).
3. C sourcecode by CodeBuster
static int verify_checksum(char *credit_card)
{
char *cp;
int dbl;
int check_sum;
check_sum = 0;
dbl = 0;
cp = credit_card + strlen(credit_card) -1;
while (cp >= credit_card)
{
int c;
c = *cp-- -'0';
if (dbl)
{
c *= 2;
if (c >= 10)
c -= 9;
}
check_sum += c;
dbl = !dbl;
}
return ((check_sum % 10) == 0);
}
//eof