Reply
Highlighted
Member
Posts: 3
Registered: ‎07-17-2019
Accepted Solution

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
Highlighted
Solution
Accepted by topic author Praveens
‎06-09-2020 10:32 PM
Posts: 459
Topics: 0
Kudos: 91
Blog Posts: 0
Ideas: 0
Solutions: 36
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;            
}

  

Powered by NexWebSites.com -
Certified Authorize.net developers

View solution in original post


All Replies
Highlighted
Administrator
Posts: 141
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.

Highlighted
Solution
Accepted by topic author Praveens
‎06-09-2020 10:32 PM
Posts: 459
Topics: 0
Kudos: 91
Blog Posts: 0
Ideas: 0
Solutions: 36
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;            
}

  

Powered by NexWebSites.com -
Certified Authorize.net developers