Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Validate credit card number with Mod 10 algorithm

0.00/5 (No votes)
15 Jan 2013 1  
Article provides details about Mod 10 algorithm and how to validate a credit card number with mod 10 algorithm using C#.

Introduction

All you know what information contains in your NIC number. But do you know what information contains in the Credit Card Number? Following article provides brief details about what information contain in your credit card and demonstrates to how to validate credit card number using mod 10 (Luhn) algorithms with C#.

Background 

Card Length 

Typically, credit card numbers are all numeric and the length of the credit card number is between 12 digits to 19 digits. 

  • 14, 15, 16 digits – Diners Club
  • 15 digits – American Express
  • 13, 16 digits – Visa
  • 16 digits - MasterCard  

For more information please refer http://en.wikipedia.org/wiki/Bank_card_number.

Hidden information

  1.  Major Industry Identifier (MII) 
  2. The first digit of the credit card number is the Major Industry Identifier (MII). It designates the category of the entry which issued the card.    

    • 1 and 2 – Airlines 
    • 3 – Travel
    • 4 and 5 – Banking and Financial
    • 6 – Merchandising and Banking/Financial
    • 7 – Petroleum
    • 8 – Healthcare, Telecommunications
    • 9 – National Assignment 
  3. Issuer Identification Number 
  4. The first 6 digits are the Issuer Identification Number. It will identify the institution that issued the card. Following are some of the major IINs. 

  5. Amex – 34xxxx, 37xxxx 
  6. Visa – 4xxxxxx 
  7. MasterCard – 51xxxx – 55xxxx 
  8. Discover – 6011xx, 644xxx, 65xxxx 
  9. Account Number 
  10. Taking away the 6 identifier digits and the last digits, remaining digits are the person’s account number (7th and following excluding last digits) 

  11. Check digits   

Last digit is known as check digits or checksum. It is used to validate the credit card number using Luhn algorithm (Mod 10 algorithm). 

For more information refer: http://en.wikipedia.org/wiki/Bank_card_number and http://en.wikipedia.org/wiki/List_of_Issuer_Identification_Numbers.

Luhn algorithm (Mod 10)  

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, IMEI numbers, National Provider Identifier numbers in US and Canadian Social Insurance Numbers. It was created by IBM scientist Hans Peter Luhn. (http://en.wikipedia.org/wiki/Luhn_algorithm)

When you implementing eCommerce application, It is a best practice validating credit card number before send it to the bank validation.

Here are the Luhn steps which can used to validate the credit card number. 

4 0 1 2 8 8 8 8 8 8 8 8 1 8 8 1  

Step 1 - Starting with the check digit double the value of every other digit (right to left every 2nd digit)  

Step 2 - If doubling of a number results in a two digits number, add up the digits to get a single digit number. This will results in eight single digit numbers.

Step 2 

Step 3 - Now add the un-doubled digits to the odd places

 

Step 4 - Add up all the digits in this number

If the final sum is divisible by 10, then the credit card number is valid. If it is not divisible by 10, the number is invalid. 

Using the code   

Following code sample validates your credit card number against Mod 10. 

public static bool Mod10Check(string creditCardNumber)
{
    //// check whether input string is null or empty
    if (string.IsNullOrEmpty(creditCardNumber))
    {
        return false;
    }

    //// 1.	Starting with the check digit double the value of every other digit 
    //// 2.	If doubling of a number results in a two digits number, add up
    ///   the digits to get a single digit number. This will results in eight single digit numbers                    
    //// 3. Get the sum of the digits
    int sumOfDigits = creditCardNumber.Where((e) => e >= '0' && e <= '9')
                    .Reverse()
                    .Select((e, i) => ((int)e - 48) * (i % 2 == 0 ? 1 : 2))
                    .Sum((e) => e / 10 + e % 10);


    //// If the final sum is divisible by 10, then the credit card number
    //   is valid. If it is not divisible by 10, the number is invalid.            
    return sumOfDigits % 10 == 0;            
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here