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)
{
creditCardNumber = Regex.Replace(creditCardNumber,@"[^0-9]","");
int cardSize = creditCardNumber.Length;
if (cardSize >= 13 && cardSize <= 16)
{
int odd = 0;
int even = 0;
char[] cardNumberArray = new char[cardSize];
cardNumberArray = creditCardNumber.ToCharArray();
Array.Reverse(cardNumberArray, 0, cardSize);
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 (temp > 9)
{
temp = temp-9;
}
even += temp;
}
}
if ((odd+even)%10 == 0)
return true;
else
return false;
}
else
return false;
}