Another credit card validator code
For the last few weeks, I’ve been working with PayPal API at my job. Besides the usual PayPal payment method, PayPal provides a direct credit card transaction solution too and it’s named Website Payments Pro. When we’re dealing with credit card numbers, we should be careful with fraudulent, illegal and false number cards. In my past experience, I’ve seen some credit card validators but never worried about how it was done. But this time, I had to take a look on how the validator works because of Discover credit card, which is not in use in Brazil.
A very quick search in the internet and I found the base algorithm to validate the most common credit card brands (Visa, MasterCard, Diners Club, Amex and Discover). It is the Luhn Algorithm. The Luhn Algorithm doubles every second digit from the rightmost of the provided number. For instance, if we have a number 529126, it will double the bolded digits. Then, it sums the doubled digits and undoubled digits ( (5×2) + 2 + (9×2) + 1 + (2×2) + 6) = 10 + 2 + 18 + 1 + 4 + 6 = (1 + 0) + 2 + (1 + 8 ) + 1 + 4 + 6 = 23 ). At last, it will verify if the resulted number is a multiple of 10.
So, let’s run into the code. The Luhn Algorithm implemented by me looks like the following:
public static bool LuhnAlgorithm(string creditCardNumber)
{
bool alt = false;
int sum = 0;
if (String.IsNullOrEmpty(creditCardNumber))
{
throw new ArgumentNullException("creditCardNumber", String.Format(CultureInfo.CurrentCulture, "the credit card number can't be null!"));
}
if (!Regex.IsMatch(creditCardNumber, @"^\d*$"))
{
throw new ArgumentException("Invalid Credit Card Number", "creditCardNumber");
}
foreach (char digit in Reverse(creditCardNumber))
{
int digitToSum = int.Parse(digit.ToString());
if (alt)
{
digitToSum = digitToSum * 2;
if (digitToSum > 9)
{
digitToSum -= 9;
}
}
sum += digitToSum;
alt = !alt;
}
return (sum % 10 == 0);
}
The Reverse method is:
public static string Reverse(string s)
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
Now that we have the credit card number validator, we should check if the number is valid for the provided brand. To accomplish this task, we will use Regular Expressions. The table below shows how each brand implements their credit card numbers:
| Card Type | Prefix | Length | Regular Expression |
| AMEX | 34, 37 | 15 | ^3[4, 7][\d]{13}$ |
| MASTERCARD | 51-55 | 16 | ^5[1-5][\d]{14}$ |
| DINERS | 300-305 36, 38 |
14 | ^30[0-5][\d]{11}$ ^3[6,8][\d]{12}$ |
| DISCOVER | 6011 | 16 | ^6011[\d]{12}$ |
| VISA | 4 | 13 16 |
^4[\d]{12}$ ^4[\d]{15}$ |
And that’s it. Just validates the credit card string number with the regular expression after the Luhn Algorithm and you will have a reliable credit card validator. The complete method will be like this:
public static bool ValidateCreditCardNumber(string creditCardNumber, string creditCardType)
{
if (!LuhnAlgorithm(creditCardNumber))
{
return false;
}
switch (creditCardType)
{
case "visa":
return ValidateVisa(creditCardNumber);
case "masterCard":
return ValidateMasterCard(creditCardNumber);
case "diners":
return ValidateDinersClub(creditCardNumber);
case "amex":
return ValidateAmex(creditCardNumber);
case "discover":
return ValidateDiscover(creditCardNumber);
default:
return false;
}
}
The discover validator method:
public static bool ValidateDiscover(string creditCardNumber)
{
return (Regex.IsMatch(creditCardNumber, @"^6011[\d]{12}$"));
}
Tags: C#, Credit Card, CSHARP, PayPal, Regex, Regular Expression, Validation, Validator
2 People have left comments on this post
Ya’ know, ya’ could upload the right menu header image sent to you by Mr. Tealdi.
I agree with MACSkeptic. Please, upload “NOW” the header image.