views:

2759

answers:

10

Can credit card type be determined solely from the credit card number?

Is this recommended or always ask client for the type of credit card they're using?

I googled about it and found this algorithm: http://cuinl.tripod.com/Tips/o-1.htm , is this reliable?

Thanks

+5  A: 

Yes, the site you mentioned is correct. Many sites, incl. Google Checkout I believe, rely on automatic detection of the card type. It's convenient, makes the UI less cluttered (one less input box) and saves time. Go ahead!

Alex
+1 Probably the only reason to provide a drop-down box for the card type is as a means of error checking.
Will Bickford
+4  A: 

I am pretty certain that at least for MasterCard, Visa, Discover, and American Express that that is accurate. I have never worked with any of the others.

See the very bottom of this page: http://www.merchantplus.com/resources/pages/credit-card-logos-and-test-numbers/

Also this might be useful to you" http://www.beachnet.com/~hstiles/cardtype.html

This is pretty interesting to: http://en.wikipedia.org/wiki/Bank%5Fcard%5Fnumber

AaronLS
http://www.barclaycardbusiness.co.uk/docs/binranges.pdf has BIN ranges for cards, from a UK bank, so its going to be accurate.
gbjbaanb
+9  A: 

I have heard one good reason to make them pick (even though you can figure it out). So that they know the list of credit cards you accept.

Lou Franco
+1 Knowing what cards are accepted is always good idea.
MitMaro
Additionally, it's just a good thing to be able to check against.
Matt Ball
Interesting reason, but I don't agree with it. You can easily *show* the user what cards you accept with little icons or text without making them do any extra work.
Evgeny
+2  A: 

here is the script that i use that works with current card ranges. also does a validity check on the number.

/**
* checks a given string for a valid credit card
* @returns:
*   -1 invalid
*     1 mastercard
*    2 visa
*    3 amex
*    4 diners club
*    5 discover
*    6 enRoute
*    7 jcb
*/
function checkCC(val) {
    String.prototype.startsWith = function (str) {
     return (this.match("^" + str) == str)
    }

    Array.prototype.has=function(v,i){
     for (var j=0;j<this.length;j++){
      if (this[j]==v) return (!i ? true : j);
     }
     return false;
    }

    // get rid of all non-numbers (space etc)
    val = val.replace(/[^0-9]/g, "");

    // now get digits
    var d = new Array();
    var a = 0;
    var len = 0;
    var cval = val;
    while (cval != 0) {
     d[a] = cval%10;
     cval -= d[a];
     cval /= 10;
     a++;
     len++;
    }

    if (len < 13)
     return -1;

    var cType = -1;

    // mastercard
    if (val.startsWith("5")) {
     if (len != 16)
      return -1;
     cType = 1;
    } else
    // visa
    if (val.startsWith("4")) {
     if (len != 16 && len != 13)
      return -1;
     cType = 2;
    } else
    // amex
    if (val.startsWith("34") || val.startsWith("37")) {
     if (len != 15)
      return -1;
     cType = 3;
    } else
    // diners
    if (val.startsWith("36") || val.startsWith("38") || val.startsWith("300") || val.startsWith("301") || val.startsWith("302") || val.startsWith("303") || val.startsWith("304") || val.startsWith("305")) {
     if (len != 14)
     return -1;
     cType = 4;
    } else
    // discover
    if (val.startsWith("6011")) {
     if (len != 15 && len != 16)
      return -1;
     cType = 5;
    } else
    // enRoute
    if (val.startsWith("2014") || val.startsWith("2149")) {
     if (len != 15 && len != 16)
      return -1;
     // any digit check
     return 6;
    } else
    // jcb
    if (val.startsWith("3")) {
     if (len != 16)
     return -1;
     cType = 7;
    } else
    // jcb
    if (val.startsWith("2131") || val.startsWith("1800")) {           

     if (len != 15)
     return -1;
     cType = 7;
    } else
    return - 1;
    // invalid cc company

    // lets do some calculation
    var sum = 0;
    var i;
    for (i = 1; i < len; i += 2) {
     var s = d[i] * 2;
     sum += s % 10;
     sum += (s - s%10) /10;
    }

    for (i = 0; i < len; i += 2)
     sum += d[i];

    // musst be %10
    if (sum%10 != 0)
     return - 1;

    return cType;
}
Niko
how outdated? Thanks.
Henry
was my bad... i just remembered using that function and having to update some exception because of customer complains (some valid cc# didnt come through) - but that was actually due to the length check
Niko
Most Cards are 16# in length (UK) Maestro can be up to 19 so length checks become a PITA.
Chris M
A: 

Wikipedia contains a list of most card prefixes. Some cards are missing from the link you posted. It also appears that the link you provided is valid.

One reason to ask for the card type is for extra validation, compare what the user provided against the number.

MitMaro
+5  A: 

As a consumer, I hate choosing a card first. I want to just start typing the number.

This issue is discussed in Wroblewski's Web Form Design on pages 153-154. It's in the section "Removing Questions" of the chapter "Unnecessary Inputs." The example given is Paypal, which highlights the type of card when you've typed in your number.

Nosredna
And what happens if they don't accept Amex (as often happens) ? Do you list the cards you don't accept ?
Brian Agnew
Paypal has graphic representations for all allowed card types shown but ghosted. When you type the number, the matching card brightens to full intensity.
Nosredna
A: 

This implementation in Python should work for AmEx, Discover, Master Card, Visa:

def cardType(number):
    number = str(number)
    cardtype = "Invalid"
    if len(number) == 15:
        if number[:2] == "34" or number[:2] == "37":
            cardtype = "American Express"
    if len(number) == 13:
        if number[:1] == "4":
            cardtype = "Visa"
    if len(number) == 16:
        if number[:4] == "6011":
            cardtype = "Discover"
        if int(number[:2]) >= 51 and int(number[:2]) <= 55:
            cardtype = "Master Card"
        if number[:1] == "4":
            cardtype = "Visa"
    return cardtype
T Pops
Credit cards arent too bad as they follow a set of rules; we have Maestro cards which cause all the issues as they use the same start codes as Credit Card producers and have over 16digits.
Chris M
A: 

If all the credit cards that you accept have the same properties then just let the user enter the card number and other properties (expiry date, CVV, etc). However, some card types require different fields to be entered (e.g. start date or issue number for UK Maestro cards). In those cases, you either have to have all fields, thereby confusing the user, or some Javascript to hide/show the relevant fields, again making the user experience a bit weird (fields disappearing/appearing, as they enter the credit card number). In those cases, I recommend asking for the card type first.

dovetalk
A: 

Here's a quick a dirty way to determine the card type automatically and show it to the user while they're typing.

That means a) the user doesnt have to pick it and b) they wont waste time looking for a dropdown that doesnt exist.

Very simple jQuery version for Amex, Visa and Mastercard. if you need other card types you can take the

 $('[id$=CreditCardNumber]').assertOne().keyup(function(){

        // rules taken from http://en.wikipedia.org/wiki/Credit_card_number#cite_note-GenCardFeatures-0
        var value = $(this).val();

        $('#ccCardType').removeClass("unknown");
        if ((/^4/).test(value)) {
            $('#ccCardType').html("Visa");
            return;
        }
        if ((/^5[1-5]/).test(value)) {
           $('#ccCardType').html("Mastercard");
           return;
        }
        if ((/^3[47]/).test(value)) {
           $('#ccCardType').html("Mastercard");
           return;
        }
        $('#ccCardType').html("Enter card number above");
        $('#ccCardType').addClass("unknown");
     });

This is the jQuery to accompany this (ASP.NET MVC):

  Card number: <%= Html.TextBox("PaymentDetails.CreditCardDetails.CreditCardNumber")%>
  Card Type: <span id="ccCardType" class="unknown">Enter card number above</span>

I have a css rule for .unknown to display grayed out text.

Simon_Weaver
+2  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