Credit Card Validation in C#

March 7th, 2009 | Tags: ,

This code validates credit card numbers based on prefix, length, and the Luhn Algorithm.

using System;

public enum CardType 
{ 
   MasterCard, BankCard, Visa, AmericanExpress, Discover, DinersClub, JCB
};

public sealed class CardValidator
{
   private CardValidator() {} // static only

   public static bool Validate(CardType cardType, string cardNumber)
   {
      byte[] number = new byte[16]; // number to validate

      // Remove non-digits
      int len = 0;
      for(int i = 0; i < cardNumber.Length; i++)
      {
         if(char.IsDigit(cardNumber, i))
         {
            if(len == 16) return false; // number has too many digits
            number[len++] = byte.Parse(cardNumber[i]);
         }
      }

      // Validate based on card type, first if tests length, second tests prefix
      switch(cardType)
      {
         case CardType.MasterCard:
            if(len != 16) 
               return false;
            if(number[0] != 5 || number[1] == 0 || number[1] > 5) 
               return false;
            break;
            
         case CardType.BankCard:
            if(len != 16)
               return false;
            if(number[0] != 5 || number[1] != 6 || number[2] > 1) 
               return false;
            break;
            
         case CardType.Visa:
            if(len != 16 &amp;&amp; len != 13) 
               return false;
            if(number[0] != 4) 
               return false;
            break;
            
         case CardType.AmericanExpress:
            if(len != 15) 
               return false;
            if(number[0] != 3 || (number[1] != 4 &amp;&amp; number[1] != 7)) 
               return false;
            break;
            
         case CardType.Discover:
            if(len != 16) 
               return false;
            if(number[0] != 6 || number[1] != 0 || number[2] != 1 || number[3] != 1) 
               return false;
            break;
            
         case CardType.DinersClub:
            if(len != 14) 
               return false;
            if(number[0] != 3 || (number[1] != 0 &amp;&amp; number[1] != 6 &amp;&amp; number[1] != 8)
               || number[1] == 0 &amp;&amp; number[2] > 5) 
               return false;
            break;
            
         case CardType.JCB:
            if(len != 16 &amp;&amp; len != 15) 
               return false;
            if(number[0] != 3 || number[1] != 5) 
               return false;
            break;
      }

      // Use Luhn Algorithm to validate
      int sum = 0;
      for(int i = len - 1; i >= 0; i--)
      {
         if(i % 2 == len % 2)
         {
            int n = number[i] * 2;
            sum += (n / 10) + (n % 10);
         }
         else
            sum += number[i];
      }
      return (sum % 10 == 0);
   }
}
  1. May 2nd, 2009 at 07:04
    Reply | Quote | #1

    can you provide C# code to generate valide credit cards??

    regards,
    Nitin Sawant

    • Navnit
      January 22nd, 2010 at 01:11
      Reply | Quote | #2

      public sealed class Card_Validation
      {
      string MasterCard, BankCard, Visa, AmericanExpress, Discover, DinersClub, JCB;
      private Card_Validation()
      {
      } // static only

      //public static bool Validate(CardType cardType, string cardNumber)
      public static bool Validate(string cardType, string cardNumber)
      {
      byte[] number = new byte[16]; // number to validate

      // Remove non-digits
      int len = 0;
      for(int i = 0; i 5)
      return false;
      break;

      case “BankCard”:
      if(len != 16)
      return false;
      if(number[0] != 5 || number[1] != 6 || number[2] > 1)
      return false;
      break;

      case “Visa”:
      if(len != 16 && len != 13)
      return false;
      if(number[0] != 4)
      return false;
      break;

      case “AmericanExpress”:
      if(len != 15)
      return false;
      if(number[0] != 3 || (number[1] != 4 && number[1] != 7))
      return false;
      break;

      case “Discover”:
      if(len != 16)
      return false;
      if(number[0] != 6 || number[1] != 0 || number[2] != 1 || number[3] != 1)
      return false;
      break;

      case “DinersClub”:
      if(len != 14)
      return false;
      if(number[0] != 3 || (number[1] != 0 && number[1] != 6 && number[1] != 8)
      || number[1] == 0 && number[2] > 5)
      return false;
      break;

      case “JCB”:
      if(len != 16 && len != 15)
      return false;
      if(number[0] != 3 || number[1] != 5)
      return false;
      break;
      }

      // Use Luhn Algorithm to validate
      int sum = 0;
      for(int i = len – 1; i >= 0; i–)
      {
      if(i % 2 == len % 2)
      {
      int n = number[i] * 2;
      sum += (n / 10) + (n % 10);
      }
      else
      sum += number[i];
      }
      return (sum % 10 == 0);
      }
      }

  2. mexicotom
    August 4th, 2009 at 18:01
    Reply | Quote | #3

    Your Luhn algorithm is interesting, but according to my tests is 10x slower than this one:

    http://orb-of-knowledge.blogspot.com/2009/08/extremely-fast-luhn-function-for-c.html

    Also, line #23 is not compiling for me, as it complains that Byte.Parse cannot take a char, but must take a string. I then had to call .ToString() on the char value to make it work (which may account for some of the slowness mentioned above).

    • Navnit
      January 22nd, 2010 at 01:09
      Reply | Quote | #4

      if(len == 16) return false; // number has too many digits
      number[len++] =Convert.ToByte(cardNumber[i]);

  3. September 28th, 2009 at 18:21
    Reply | Quote | #5

    Thanks for the code. You really saved me some time with this.

  4. Marinus
    October 13th, 2010 at 00:27
    Reply | Quote | #6

    Can you please tell me if i have a Text box how will it be possible to send the text box text information through this class

    public static bool Validate(CardType cardType, string cardNumber)

  5. Jarrett
    November 1st, 2011 at 14:03
    Reply | Quote | #7

    Can you build the different classes used to validate each card. I was thinking that a separate class for each card would be needed for validation.

TOP