Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify card type from card number [duplicate]

i have an array of card types that looks something like this

var cards = new Array();

cards [0] = {name: "VISA", length: "13,16", prefixes: "4", checkdigit: true};
cards [1] = {name: "VISA_DELTA/ELECTRON", length: "16", prefixes: "417500,4917,4913",     checkdigit: true};

however id like to be able to find the card type depending on the card number entered. e.g if they select their card type from a drop down list i.e visa, then the credit card number should start with 4 otherwise when they submit the form a message of some sort should be display saying this is (whatever card it is, please change card type). Any help would be appreciated.

the id for the card number text field is CardNumber. Im not sure what other info may be needed, i have a function called Validate which validates the rest of the form and a function call Calculate which does the luhn check.

like image 746
Ghatzi Avatar asked May 06 '11 12:05

Ghatzi


People also ask

How do you tell what type of card a number is?

Identify a bank card's credit company by looking at the first number. Cards that start with a "3" are American Express. Those that start with "4" are Visa credit and debit cards, those that start with "5" are MasterCard credit and debit cards, and those that start with "6" are Discover credit cards.

How do you tell if a card number is credit or debit?

Bottom line Curious how to know if a card is credit or debit? They look quite similar, but debit cards will inform you on either the front or back. You can also type in your debit or credit card number on the Binlist website. It'll identify the nature of your card when you enter the first six digits of the card number.

What type of card is my bank card?

You can tell if your card is a debit card by looking at the right-hand side of the card where it will say “Debit” on either the top or the bottom corner. A credit card is a bank card that enables you to make purchases now and pay for them later.

What do the first 4 digits of a credit card mean?

Visa starts with 4, a Mastercard is 5 and Discover is 6. Other numbers are used to identify the industry. For instance, 1 and 2 are used for the airline industry. The numeral 3 represents travel and entertainment, so it makes sense that 3 also indicates it's an American Express card.


4 Answers

I believe you are probably looking for something like this:

<script type="text/javascript">
function GetCardType(number)
{
    // visa
    var re = new RegExp("^4");
    if (number.match(re) != null)
        return "Visa";

    // Mastercard 
    // Updated for Mastercard 2017 BINs expansion
     if (/^(5[1-5][0-9]{14}|2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12}))$/.test(number)) 
        return "Mastercard";

    // AMEX
    re = new RegExp("^3[47]");
    if (number.match(re) != null)
        return "AMEX";

    // Discover
    re = new RegExp("^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)");
    if (number.match(re) != null)
        return "Discover";

    // Diners
    re = new RegExp("^36");
    if (number.match(re) != null)
        return "Diners";

    // Diners - Carte Blanche
    re = new RegExp("^30[0-5]");
    if (number.match(re) != null)
        return "Diners - Carte Blanche";

    // JCB
    re = new RegExp("^35(2[89]|[3-8][0-9])");
    if (number.match(re) != null)
        return "JCB";

    // Visa Electron
    re = new RegExp("^(4026|417500|4508|4844|491(3|7))");
    if (number.match(re) != null)
        return "Visa Electron";

    return "";
}
</script>

Originally posted here @ http://mel-green.com/2008/11/determine-credit-card-type-with-javascript/

like image 138
Russ Clarke Avatar answered Sep 21 '22 18:09

Russ Clarke


Here's a jQuery plugin designed for exactly this:

http://jquerycreditcardvalidator.com/

See also this StackOverflow answer for an excellent discussion of how credit card numbers are related to card types:

https://stackoverflow.com/a/72801/12484

like image 24
Jon Schneider Avatar answered Sep 19 '22 18:09

Jon Schneider


The card companies have a fairly well-defined list of prefixes which are specific to the card type.

These prefixes range from a single digit (everything begining with '5' is Mastercard) to some strings up to six or seven digits long, for more obscure card types, and also for mainstream cards, but being able to identify not only the card type but also the issuing bank.

Here's one resource I found: http://www.beachnet.com/~hstiles/cardtype.html

You may also want to see Wikipedia: http://en.wikipedia.org/wiki/Credit_card_numbers

The down side is that while the prefixes that are in circulation now are pretty much fixed, there's always the likelyhood that they'll need to come up with more, so you would need to ensure that you keep any prefix list up-to-date, especially if you're using it to check the longer prefix ranges.

like image 33
Spudley Avatar answered Sep 19 '22 18:09

Spudley


The card prefix scheme is detailed in BIN (bank id number) lists and change regularly, I would advise against validating against the full 1st 6 digits of the PAN unless you plan regular updates and only perform a cursory check on the first couple of digits (your missing 48* for for visa/electon for example and visa length can span 16-19).

If your in the UK; http://www.barclaycard.co.uk/business/documents/pdfs/bin_rules.pdf

like image 28
Alex K. Avatar answered Sep 20 '22 18:09

Alex K.