Python Script 8: Validating Credit Card Number – Luhn’s Algorithm

checking credit card number's validity using python script

The Luhn algorithm , also known as the “modulus 10” algorithm, is a checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in the United States, Canadian Social Insurance Numbers and Israel ID Numbers.

Algorithm:

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.

Generating check digit:

  • Lets assume you have a number given below:
    3 – 7 – 5 – 6 – 2 – 1 – 9 – 8 – 6 – 7 – X
    X is the check digit.
  • Now starting from the right most digit i.e. check digit, double the every second digit.
    New number will be:
    3 – 14 – 5 – 12 – 2 – 2 – 9 – 16 – 6 – 14 – X
  • Now if double of a digit is more then 9, add the digits.
    So the number will become:
    3 – 5 – 5 – 3 – 2 – 2 – 9 – 7 – 6 – 5 – X
  • Now add all digits.
    47 + X
  • Multiply the non-check part by 9.
    47 * 9 = 423
  • Unit digit in the multiplication result is the check digit. X = 3
  • Valid number would be 37562198673.
Validating the generated number:

You can use tools available online to validate that the number generated is valid as per Luhn’s algorithm or not.

You can validate the number by visiting this site.

Python Script to validate credit card number:
 

Code is available on github.

 

Host your Django application for free.

 

(Visited 930 times, 1 visits today)

You must read this :

Leave a Reply

Your email address will not be published. Required fields are marked *