12,400,556 members (68,920 online)
alternative version

51.3K views
14 bookmarked
Posted

# Simple Mod 10 validation for creditcards

, 7 May 2005
 Rate this:
This article describes you an easy way of validating a credit card.

## Introduction

This article demonstrates a simple way of detecting errors while entering the credit card in a form. This method is popularly known as Mod 10 check. This saves a lot of time and money for ecommerce businesses by avoiding a trip to the banks. This program uses an Mod-10 Method (ISO 2894/ANSI 4.13) to verify the validity of a credit card number.

According to this method, starting from the last digit of the credit card number, multiply every second number by two. If the resulting number is greater than 9, substract 9 from that number to make it a single digit. Now add all the numbers and is taken modulo with 10. If the result is 0, then the credit card number is valid.

```private bool ValidateCreditCardNumber(string creditCardNumber)
{
//Replace any character other than 0-9 with ""
creditCardNumber = Regex.Replace(creditCardNumber,@"[^0-9]","");

int cardSize = creditCardNumber.Length;

//Creditcard number length must be between 13 and 16
if (cardSize >= 13 && cardSize <= 16)
{
int odd = 0;
int even = 0;
char[] cardNumberArray = new char[cardSize];

//Read the creditcard number into an array
cardNumberArray = creditCardNumber.ToCharArray();

//Reverse the array
Array.Reverse(cardNumberArray, 0, cardSize);

//Multiply every second number by two and get the sum.
//Get the sum of the rest of the numbers.
for (int i = 0; i < cardSize; i++)
{
if (i%2 ==0)
{
odd += (Convert.ToInt32(cardNumberArray.GetValue(i))-48);
}
else
{
int temp = (Convert.ToInt32(cardNumberArray[i]) - 48) * 2;
//if the value is greater than 9, substract 9 from the value
if (temp > 9)
{
temp = temp-9;
}
even += temp;
}
}
if ((odd+even)%10 == 0)
return true;
else
return false;
}
else
return false;
} ```

A list of licenses authors might use can be found here

## Share

 Web Developer United States
No Biography provided

## You may also be interested in...

 First Prev Next
 Regarding array.getvalue(i) - 48 kaibin873-Jul-06 12:31 kaibin87 3-Jul-06 12:31
 Re: Regarding array.getvalue(i) - 48 Oryan K7-Sep-06 15:00 Oryan K 7-Sep-06 15:00
 Variable Card length ? mgmail17-Apr-06 10:46 mgmail 17-Apr-06 10:46
 what modifications need to be done to convert this algorythim into a variable card length MOD 10 check? for instance, hypothetically speaking if i had a 6 digit credit card or a 22 digit credit card.
 Re: Variable Card length ? Oryan K7-Sep-06 15:07 Oryan K 7-Sep-06 15:07
 And now for the source code ..... fwsouthern10-May-05 16:02 fwsouthern 10-May-05 16:02
 Re: And now for the source code ..... Chris Maunder10-May-05 16:37 Chris Maunder 10-May-05 16:37
 Last Visit: 31-Dec-99 18:00     Last Update: 26-Jul-16 21:11 Refresh 1