LUHN Algorithm in C# (Validate Credit Card)

Posted by Jiltin     18 November, 2008    10,290 views   

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 and Canadian Social Insurance Numbers.

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. This account number must pass the following test:

1. Counting from rightmost digit (which is the check digit) and moving left, Double the value of every alternate digit. For any digits that thus become 10 or more, take the two numbers and add them together. For example, 1111 becomes 2121, while 8763 becomes 7733 (from 2×6=12 → 1+2=3 and 2×8=16 → 1+6=7).
2. Add all these digits together. For example, if 1111 becomes 2121, then 2+1+2+1 is 6; and 8763 becomes 7733, so 7+7+3+3 is 20.
3. If the total ends in 0 (put another way, if the total modulus 10 is congruent to 0), then the number is valid according to the Luhn formula; else it is not valid. So, 1111 is not valid (as shown above, it comes out to 6), while 8763 is valid (as shown above, it comes out to 20).

/// <summary>
/// Validates a credit card number using the standard Luhn/mod10
/// validation algorithm.
/// </summary>
/// <param name="cardNumber">Card number, with or without
///        punctuation</param>
/// <returns>True if card number appears valid, false if not
/// </returns>
public bool IsCreditCardValid(string cardNumber)
{
   const string allowed = "0123456789";
   int i;

   StringBuilder cleanNumber = new StringBuilder();
   for (i = 0; i < cardNumber.Length; i++)
   {
      if (allowed.IndexOf(cardNumber.Substring(i, 1)) >= 0)
         cleanNumber.Append(cardNumber.Substring(i, 1));
   }
   if (cleanNumber.Length < 13 cleanNumber.Length > 16)
      return false;

   for (i = cleanNumber.Length + 1; i <= 16; i++)
      cleanNumber.Insert(0, "0");

   int multiplier, digit, sum, total = 0;
   string number = cleanNumber.ToString();

   for (i = 1; i <= 16; i++)
   {
      multiplier = 1 + (i % 2);
      digit = int.Parse(number.Substring(i – 1, 1));
      sum = digit * multiplier;
      if (sum > 9)
         sum -= 9;
      total += sum;
   }
   return (total % 10 == 0);
}

How To Generate Valid Credit Card Numbers

Following Google Searches Lead To This Post: c# luhn check
sin number verification using c#
Luhn Algorithm SIN validation algorithm
algo de luhn c#
luhn generator
Luhn algorithm c#
Luhn validation c#
luhn-10 calculate
vb.net luhn check function
credit card algorithm generator
“Error validating card/account number checksum”
standard modulus 11 checksum
credit card no generator in c#.net
c modulus 11 generator
sin card algorithm
luhn check sample number
luhn validation credit card php
luhn C#
generate mod11 checksum
modulus 11 checksum c
Luhn Mod-10 Method c#
credit card validation algorithm
C# validate code algorithm
VALIDATE CREDIT CARDS C# code
Luhn algorithm vb.net
verify mod 10 algorithm with c#
C# visa card generator
luhn check vb.net
Luhn Algorithm SIN
Luhn c# get last digit
Luhn Mod10 c#
credit card algorithim
credit card validity c example programming
Mod 10 check digit C#
“formula validation” source code
C# mod checksum
C# mod11 algorithm
luhn mod 10 c#
LUHN credit card identification
LUHN formula+c#
c# valid double
modulus 11 in c#
fast c# checksum algorithm
C# MOD11 check digit REGX
c# validate credit card
C# validate credit card number
visa generator js algorithm
luhn algorithm tester
how to generate sin card
mod 43 check digit C# example
.net luhn
luhn mod 10 algorithm .net
c# sin validator
java algorithm modulo sum 2 checksum
“luhn algorithm” “generation” “sql”
generate MOD10 number in JAVA
c# luhn algorithm
c# mod two numbers algorithm
cc validation function mod10 c#
validation code for identity card number in c#
validate mod 10 c#
EXAMPLE OF CREADIT CARD IN.COM
luhn algorithm c
luhn algorithm valid cc generator
c# validate credit cards
how to create luhn code
how to create luhn check digit
credit card number generator ALGORITHM IN c#
credit card number generator algo
oracle pl/sql “mod 10″ algorithm
“SIN Validator” c#
Luhn/mod10 validation algorithm for 17 digit card numbers
luhn algorithm 4 – visa
c# check digit mod 10
asp.net c# validator credit card number
asp.net c# validator basic creditcard number
test card passes luhn
luhn mod 10 check digit c#
sample cc number to pass luhn test
creditcard validation C#
what is standard luhn
c# credit card validation
mod11 C#
C# mod11
calculate modulus number c#
c# luhn checksum
c# luhn algo
LUHN c#
asp.net luhn algorhithm
sine generator C#
c# luhn
c# asp.net “credit card” validation .net 2009 LUHN
visual basic luhn algorithm
luhn algorithm vb
mod10 check vb.net
algoritmo luhn
creditcard lenght
c# mod10 verification
sin luhn
credit card validation luhn algorithm using c#
19 digit credit card sample number
luhn algorithm generation
credit card checksum c#
Validated using Luhn’s Mod 10 algorithm in c#
sin check c#
19 digit luhn check formula
C# LUHN formula
LUHN10 check C#
luhn algorithm in c
credit card c++ luhn
calculate digit modulus 11 php
C# ALOGARITIMO COM STRING
c# isbn code generator algorithm
luhn algorithm c# library
luhn algorithm c# library generate valid credit card number
standard Luhn checksum test
c# check is linux
java luhn calculation
validate creditcard checksum open source .NET
luhn10 algorithm + c#
generate canadian social insurance number c#
“Visual Basic 2008″ Luhn’s algorithm
sin card number validation
Luhn formula c source
c# to validate mod 10
luhn validation
luhn example
credit card generator 10 nov 2009
example credit card number that passes Luhn Check
asp.net luhn check
shell script generate check digit
validate credit card c#
Luhn generation code c#
Luhn formula and Visa Card
validation credit card algorithm in C
credit card algorithm
credit card number check digit c#
vb cleannum() cear decimals
mod 11 algorithm cards
credit card number generator c#
c# generate luhn check digit
luhn check c#
C# algorithm to check credit card number
validate number of digits c#
luhn alogorithem in c#
visa modulus check formula
c# mod 10 credit card code
c# validation regular expression standards
canadian sin validation formula
generating numbers and char Cart C#
.net algorithm to check credit card number validity
c# calculate luhn checkdigit
algorithm generator of c# function
Test latest visa card number with 17 digits
test 17 digits visa card number
c# luhn generation
Luhn Mod-10 to validate credit card numbers c# code
luhn’s algorithm to validate credit card using vb
luhn formula c#
LUHN 19 digit
social insurance number validity c# program
“isbn” number “c#” algorithm
how to sin checksum a number in java
sin check digit algorithm
how to mod 11 c#
luhn formula code in js
luhn check digit C++
bank account LUHN check digit
calculate luhn check digit vb
how to create luhn mod 10 check digit
Luhn Mod-10 test algorithm c#
csharp mod10
c# credit card regex validation
luhns modulus cc validator
The sum of digits of a number using C#
c# credit card LUHN algorithm
c# credit card Security Code regex validation
Algoritmo LUHN 10 vb.net
how to generate credit card numbers using the luhn10
credit card validator using luhn’s formulae in c# asp.net
luhn algorithm credit card c#
luhn algorithm credit card c# 20 digits
+modulus +11 +c#
luhn’s algorithm in c#
validate account number vb.net
algo luhn c#
calc luhn sql server
script of credit in asp.net
c++ algorithm for sin
visa validate security code numbers LUHN formula
validate credit card number c
c# credit card security code checker
java visa card validation regex
c# luhn10
csharp code for luhn check
Validates Credit card Numbers generator
c# mod 11 check digit
validate credit card number C#
validador algoritmo luhn
validatecreditcard checksum
credit card security code algorithm
LUHN Formula c# code
+c# credit card validation
luhn algorithm +C# code
luhns modulus for validating cc
c# sine generator
cc mod 10 check algorithm
Mod10(Luhn) C#
sample c# application to validate all credit cards
credit card validation code c#
luhn algorithm java generator
algoritmo mod10
C#, luhns algorithm
how to validate credit card number in c#
vb code validates a credit-card using Luhn’s algorithm
standard Luhn/mod10 validation algorithm
mod 10 algorithm in c programing
c# mod 10 validation
is luhn c#
Luhn Algorithm in 13 digit credit card number
bank account number validation in asp.net c#
modulus 10 algorithm .net
c# isbn validate
C# ALGORITHM mod11
C# modulus 11 algorithm
luhn in c
credit card mod 10 algorithm c#
simple checksum function C#
luhn10 algorithm
luhn algorithm c sharp
validate luhn generator
credit card vb.net validation
luhn algo for credit card validation
algo de verif email c#
sin formule C#
visa “17 digits” new
“latest visa card number with 17 digits”
“17 digits visa”
come generare checksum formula luhn
vb validate credit card using luhn’s
credit card Luhn Algorithm valid
create luhn algorithm c#
modulo 11 algorithm
c# total sum algorithm
Mod 10 Luhn algorithm sql
algoritmo de luhn card security number
canadian sin c#
example code for modulo 10 algorithm in c++
16 digit luhn number
example c++ code mod10 algorithm
c# canadan morgage formula
c# credit card number validator
checkdigit c#
standard mod 10 algoritem
how to validate formula in c#.net
Luhn formula (MOD 10 check) credit card number
algorithme de luhn c#
.net mod calculation c#
17 digit visa cards
como hacer algoritmo Luhn en vb
algoritmo Luhn vb 6.0
create an algorithm c#
Luhn algorithm credit card validation
luhn algorithm with security code
sample program to verify the SIN number
luhn algorithmus c#
create luhn c++
c# modulo 11
generate mod 10 check digit c sharp
canadian sin algorithm
luhn algorithm generator
how to test and validate an algorithm
valid canadian social insurance number csharp source code
CALCULATE MOD IN C#
calculate check digits C#
C# +mod10
credit card validation algo in C#
modulo 10 validation c#
csharp mod10 function
tester algorithme luhn c#
visa number generator c#
digits on sin card
“credit card” “security code” algorithm
luhn c# code example
csharp sum digits
Canadian mortgage formula in c sharp
bank account Modulus Checking formula
luhn mod 10 c++
java program sin validation
How to validate Identity Numbers in c#
Luhn numbers cc
canada mortgage formula c#
SIN validation in C#
C# find if credit card is a visa
algoritmo de luhn
c# STANDARD LUHNS MODULUS 10

Post to Twitter  Post to Delicious  Post to Digg    Post to StumbleUpon

Categories : Web & Scripts Tags : , , ,

Comments
Posted by mexicotom
August 4, 2009

mexicotom,

Great! Thank you for the reference.

Jiltin

Posted by Jiltin
December 8, 2009

This is really cool, and I cannot wait to try it. I will have to spread the word.

Posted by mac wholesale
December 11, 2009

I found good resources of c#. Check this out

http://csharptalk.com

Posted by Rahul
Leave a comment

(required)

(required)