Integration and Testing

Authorize.Net API questions and help with your payment integration.

Member
Posts: 3
Registered: ‎07-17-2019

Validate Credit card number

Hi, I am using the C# integration to do the payment, i want to know the minimum lenght of all credit card types to validate in my BLL method.

Accepted Solutions
Solution
Accepted by topic author Praveens
‎06-09-2020 10:32 PM
Posts: 465
Topics: 0
Kudos: 92
Blog Posts: 0
Ideas: 0
Solutions: 37
Registered: ‎04-28-2017

Re: Validate Credit card number

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

For C#, the following validates a credit card number using the Luhn (Mod 10 algorithm):

```public static bool Mod10Check(string creditCardNumber)
{
if (string.IsNullOrEmpty(creditCardNumber))
{
return false;
}

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);

return sumOfDigits % 10 == 0;
}```

Certified Authorize.net developers

All Replies
Posts: 163
Registered: ‎11-29-2017

Re: Validate Credit card number

There have been cards issued that are only 12 characters. Most cards are 16 digits. The standard way of validating is to use the Luhn algorithm. There's a lot of free sample code out there with it that you can incorporate.

Solution
Accepted by topic author Praveens
‎06-09-2020 10:32 PM
Posts: 465
Topics: 0
Kudos: 92
Blog Posts: 0
Ideas: 0
Solutions: 37
Registered: ‎04-28-2017

Re: Validate Credit card number

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

For C#, the following validates a credit card number using the Luhn (Mod 10 algorithm):

```public static bool Mod10Check(string creditCardNumber)
{
if (string.IsNullOrEmpty(creditCardNumber))
{
return false;
}

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);

return sumOfDigits % 10 == 0;
}```