tags:

views:

11395

answers:

14

I'm trying to figure out how to detect the type of credit card based purely on it's number. Does anyone know of a definitive, reliable way to find this?

+22  A: 

http://www.merriampark.com/anatomycc.htm but no guarantees as to accuracy :)

chessguy
Chessguy's link is excellent. Relevant table from that page answered my question perfectly.
SpacePenguin
+5  A: 

Using a regular expression. Check out this link for more information.

senfo
+4  A: 

This wikipedia article may be helpful in your search: Credit Card Numbers

It looks like there are some standard prefixes that are used which could determine what the card type is.

Craig
+3  A: 

The details are all on Wikipedia: http://en.wikipedia.org/wiki/Credit_card_numbers

Sten Vesterli
+1  A: 

I saw this post a while ago it covers Visa, Master Card, American Express, Diners Club, and discover. It also gives regex's to detect them and validate them: http://patelnirav.blogspot.com/2008/04/something-about-credit-card-validations.html

Also a wiki article: http://en.wikipedia.org/wiki/Credit_card_numbers

Ryan P
both your links are the same :)
Aeon
+6  A: 

There's a good summary table in Wikipedia, at http://en.wikipedia.org/wiki/Credit_card_numbers. It's the first one to six digits that tell the type and issuer of the card.

Alex
A: 

The regex method shown is pretty ironclad and simple...but bear in mind that it's not absolutely foolproof. There are a few circumstances where the card issuer can deviate from the industry norm and goof up the logic of the regex. It's probably accurate 98ish percent of the time....

A: 

What do you mean "type of credit card"?

Credit card numbers have a structure similar to: BIN (Bank Identification Number, 6 digits) + Account number (7 digits, sequential under BIN) + Card number (2 digits, sequential under Account number) + Check digit (1 digit calculated using Luhn's algorithm). Total = 6 + 7 + 2 + 1 = 16 digits. Other structures also exist.

For example: 433473 0000019 01 1
BIN = 433473
Account Number = 0000019
Card Number = 01
Check digit = 1

The BIN identifies the issuer, i.e. bank who issued the card. Banks may have a lot of BINs and use them for different purposes. There is no way for you to know for which purpose was this particular card issued.

Visa or MasterCard or whoever is the international brand for the credit card don't know anything about the card, either. They only know which bank the BIN belongs to.

Alvaro Rodriguez
A: 

I wouldn't use a regex other than to pull out the first numeric group, you can generally tell just from the first 4 numbers (in the US). Also before bothering to pay for clearing a charge run a Mod 10 checksum on the card number to make sure it could be legitimate.

Luhn algorithm

Dan Blair
A: 

I think this is correct (not sure 100%) .. but this data used in production environment to check card type

Visa usually start with 49,44 or 47

Visa electron : 42,45,48,49

Mastercard : 51

Amex :34

Diners : 30,36,38

JCB : 35

Shoban
+1  A: 

This article talks about the Luhn algorithm for valid credit card numbers

http://www.thetaoofmakingmoney.com/2007/04/12/324.html

A: 

Here's Complete C# or VB code for all kinds of CC related things on codeproject.

  • IsValidNumber
  • GetCardTypeFromNumber
  • GetCardTestNumber
  • PassesLuhnTest

This article has been up for a couple years with no negative comments.

Simon_Weaver
@barett - fixed it. looks like they moved it from 'aspnet' category to 'validation' category which changed the link
Simon_Weaver
+2  A: 

Check this out:

http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CC70060A01B

function isValidCreditCard(type, ccnum) {
   if (type == "Visa") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "MC") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Disc") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "AmEx") {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   } else if (type == "Diners") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   }
   if (!re.test(ccnum)) return false;
   // Remove all dashes for the checksum checks to eliminate negative numbers
   ccnum = ccnum.split("-").join("");
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}
Rashy