Tuesday, 31 March 2015

How to validate a Credit Card Number?

How to validate a Credit Card Number?

Most credit card number can be validated using the Luhn algorithm, which is more or a less a glorified Modulo 10 formula!

The Luhn Formula:

  • Drop the last digit from the number. The last digit is what we want to check against
  • Reverse the numbers
  • Multiply the digits in odd positions (1, 3, 5, etc.) by 2 and subtract 9 to all any result higher than 9
  • Add all the numbers together
  • The check digit (the last number of the card) is the amount that you would need to add to get a multiple of 10 (Modulo 10)

Luhn Example:

Step Total
Original Number: 4 5 5 6 7 3 7 5 8 6 8 9 9 8 5 5
Drop the last digit: 4 5 5 6 7 3 7 5 8 6 8 9 9 8 5
Reverse the digits: 5 8 9 9 8 6 8 5 7 3 7 6 5 5 4
Multiple odd digits by 2: 10 8 18 9 16 6 16 5 14 3 14 6 10 5 8
Subtract 9 to numbers over 9: 1 8 9 9 7 6 7 5 5 3 5 6 1 5 8
Add all numbers: 1 8 9 9 7 6 7 5 5 3 5 6 1 5 8 85
Mod 10: 85 modulo 10 = 5 (last digit of card)

List of credit card number formats:

Credit Card Issuer               Starts With ( IIN Range )       Length ( Number of digits )

American Express              34, 37                                                 15


Diners Club                        300, 301, 302, 303, 304,
                                           305                                                     14
Carte Blanche

Diners Club -                     36                                                       14
International

Diners Club -                      54                                                       16
USA & Canada

Discover                            6011, 622126 to 622925,644, 
                                           645, 646, 647, 648, 649, 65              16

InstaPayment                    637, 638, 639                                     16

JCB                                    3528 to 3589                                      16

Laser                                 6304, 6706, 6771, 6709                     16-19

Maestro                             5018, 5020, 5038, 5893, 
                                           6304, 6759, 6761, 6762,
                                           6763                                                   16-19

MasterCard                        51, 52, 53, 54, 55                               16-19

Visa                                     4                                                         13-16

Visa Electron                     4026, 417500, 4508, 
                                           4844, 4913, 4917                               16


No comments:

Post a Comment