12,757,549 members (41,802 online)
Tip/Trick
alternative version

#### Stats

142K views
28 bookmarked
Posted 29 Dec 2012

# Validate credit card number with Mod 10 algorithm

, 15 Jan 2013 CPOL
 Rate this:
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

#### 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).

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 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;
}```

## Share

 Technical Lead Eyepax IT Consulting (Pvt) Ltd. Sri Lanka
Having more than 9 year hands-on industry experience in software development
Responsible for designing, implementing and managing complex software systems with stringent up-time requirement.

Visit my blog

## You may also be interested in...

 Pro Pro

 First Prev Next
 So close, but wrong - you need to use the checksum correctly! Chaz.NYC28-Oct-16 9:58 Chaz.NYC 28-Oct-16 9:58
 My vote of 5 Member 1199207113-Oct-15 11:22 Member 11992071 13-Oct-15 11:22
 Re: My vote of 5 Tharaka MTR15-Oct-15 8:24 Tharaka MTR 15-Oct-15 8:24
 Good job bro.. HBK_SHAAN19-Aug-15 8:11 HBK_SHAAN 19-Aug-15 8:11
 VB.Net version kozchris12-Feb-15 7:23 kozchris 12-Feb-15 7:23
 Validation and the Law SlaveDrivinBoss12-Dec-14 12:42 SlaveDrivinBoss 12-Dec-14 12:42
 Re: Validation and the Law Member 300191019-Dec-14 12:31 Member 3001910 19-Dec-14 12:31
 Re: Validation and the Law Member 1199207113-Oct-15 11:00 Member 11992071 13-Oct-15 11:00
 Re: Validation and the Law Member 1270317425-Aug-16 4:04 Member 12703174 25-Aug-16 4:04
 Why do you need to subtract each digit by 48? Richard Hu12-Aug-14 22:13 Richard Hu 12-Aug-14 22:13
 Re: Why do you need to subtract each digit by 48? Tharaka MTR13-Aug-14 9:34 Tharaka MTR 13-Aug-14 9:34
 Re: Why do you need to subtract each digit by 48? Richard Hu13-Aug-14 16:00 Richard Hu 13-Aug-14 16:00
 My vote of 5 Grant Winney23-Jul-14 13:37 Grant Winney 23-Jul-14 13:37
 Re: My vote of 5 Tharaka MTR23-Jul-14 22:52 Tharaka MTR 23-Jul-14 22:52
 Lamda majed samyal18-Sep-13 1:46 majed samyal 18-Sep-13 1:46
 Re: Lamda Tharaka MTR19-Sep-13 0:50 Tharaka MTR 19-Sep-13 0:50
 Distinction Between Frequent Flyer and Credit Cards Predrag Kos5-Jun-13 3:45 Predrag Kos 5-Jun-13 3:45
 My vote 5 Yaseer Arafat1-Jun-13 12:46 Yaseer Arafat 1-Jun-13 12:46
 Re: My vote 5 Tharaka MTR2-Jun-13 3:15 Tharaka MTR 2-Jun-13 3:15
 My vote of 5 Rohan Leuva18-Apr-13 2:45 Rohan Leuva 18-Apr-13 2:45
 Re: My vote of 5 Tharaka MTR18-Apr-13 2:50 Tharaka MTR 18-Apr-13 2:50
 thanks you very much benjamin9x15-Apr-13 16:34 benjamin9x 15-Apr-13 16:34
 Re: thanks you very much Tharaka MTR5-Jun-13 20:33 Tharaka MTR 5-Jun-13 20:33
 Alternative method. George Swan17-Jan-13 8:55 George Swan 17-Jan-13 8:55
 Re: Alternative method. Tharaka MTR17-Jan-13 9:56 Tharaka MTR 17-Jan-13 9:56
 Last Visit: 31-Dec-99 19:00     Last Update: 23-Feb-17 21:31 Refresh 1