Click here to Skip to main content
Click here to Skip to main content

Validating Credit Card Numbers

, 12 May 2009
Rate this:
Please Sign up or sign in to vote.
Some code to process credit card orders.


When using ASP.NET to process online credit card orders, it is a good idea if you can perform some sort of validation on the credit card number before submitting it to your processor. I recently had to write some code to process credit card orders, and thought I’d share a bit of my code.

Fortunately, credit card numbers are created in a way that allows for some basic verification. This verification does not tell you if funds are available on the account, and it certainly doesn’t tell whether or not the person submitting the order is committing credit card fraud. In fact, it’s possible that the card number is mistyped in such a way that it just happens to pass verification. But, it does catch most typing errors, and reduces bandwidth usage by catching those errors before trying to actually process the credit card.

To validate a credit card number, you start by adding the value of every other digit, starting from the right-most digit and working left. Next, you do the same thing with the digits skipped in the first step, but this time you double the value of each digit and add the value of each digit in the result. Finally, you add both totals together, and if the result is evenly divisible by 10, then the card number has passed the validation.

Of course, this would be clearer with a bit of code, and Listing 1 shows my IsCardNumberValid method.

public static bool IsCardNumberValid(string cardNumber)
  int i, checkSum = 0;


  // and compute checksum of resulting digits
    while (val > 0)
      checkSum += (val % 10);
      val /= 10;

  return ((checkSum % 10) == 0);
Listing 1: Validating a credit card

The IsCardNumberValid method assumes that all spaces and other non-digit characters have been stripped from the card number string. This is a straightforward task, but Listing 2 shows the method I use for this.

public static string NormalizeCardNumber(string cardNumber)
  if (cardNumber == null)
    cardNumber = String.Empty;

  StringBuilder sb = new StringBuilder();

  foreach (char c in cardNumber)
    if (Char.IsDigit(c))

  return sb.ToString();

You will also be able to reduce bandwidth if you can avoid trying to submit a card that is not supported by the business. So, another task that can be useful is determining the credit card type.

public enum CardType
  Unknown = 0,
  MasterCard = 1,
  VISA = 2,
  Amex = 3,
  Discover = 4,
  DinersClub = 5,
  JCB = 6,
  enRoute = 7

// Class to hold credit card type information
private class CardTypeInfo
  public CardTypeInfo(string regEx, int length, CardType type)
    RegEx = regEx;
    Length = length;
    Type = type;

  public string RegEx { get; set; }
  public int Length { get; set; }
  public CardType Type { get; set; }

// Array of CardTypeInfo objects.
// Used by GetCardType() to identify credit card types.
private static CardTypeInfo[] _cardTypeInfo =
                   14, CardType.DinersClub),

public static CardType GetCardType(string cardNumber)
  foreach (CardTypeInfo info in _cardTypeInfo)
    if (cardNumber.Length == info.Length && 
        Regex.IsMatch(cardNumber, info.RegEx))
      return info.Type;

  return CardType.Unknown;
Listing 3: Determining a credit card’s type

Listing 3 is my code to determine a credit card’s type. I’m a big fan of table-driven code, when it makes sense, and so I created an array of CardTypeInfo objects. The GetCardType() method simply loops through this array, looking for the first description that would match the credit card number being tested. As before, this routine assumes all non-digit characters have been removed from the credit card number string.

The main reason I like table-driven code is because it makes the code simpler. This results in code that is easier to read and modify. GetCardType() returns a value from the CardType enum. CardType.Unknown is returned if the card number doesn’t match any card descriptions in the table.




About the Author

Jonathan Wood
Black Belt Coder
United States United States
Follow on   Twitter

Comments and Discussions

GeneralYuck PinmemberPIEBALDconsult12-May-09 6:39 



| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 12 May 2009
Article Copyright 2009 by Jonathan Wood
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid