Identify card type from card number [duplicate]
Asked Answered
A

4

26

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.

Asturias answered 6/5, 2011 at 12:13 Comment(0)
S
73

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/

Schwinn answered 6/5, 2011 at 12:19 Comment(10)
While this may answer the question, link-only answers are discouraged. If the link goes dead (link rot), this answer would contain no information at all. Consider editing this answer to include relevant details from the link.Kudu
Thanks - I did answer this over 3 years ago though. Thanks for the downvote.Schwinn
I don't see how the age of the post is germane to my suggestion, but I would certainly retract the DV if the answer were improved.Kudu
I don't want to get into a debate about this, as it's not helpful to the site nor a friendly environment for new members to experience, but the culture on S/O has changed a lot in the last couple of years; there was a time when it was generally considered Ok to post links, especially as it would feel like I was stealing if I simple just posted the content of someone else's article. I agree with the new way of doing things, but I find that retroactively downvoting something from years ago isn't helping. That's my opinion anyway.Schwinn
I understand the culture change and agree that it would be unproductive to seek out every old answer and downvote it. I was being lazy and looking for a JS card type sniffing script instead of porting my PHP one, this came up after a Google search. That's why the age doesn't matter, even old questions get traffic. So I came here, hoping for a solution, and found that My answer is in another castle, so I left the initial comment.Kudu
fwiw, you would have to put Visa electron first before visa, otherwise it would catch visa and exit. Same for Amex/DinersMorphology
Note for any others stumbling across this; Mastercard has since added a new BIN series (222100-272099), so the regex would have to be modified to take this into account. The answer this question duplicates explains that.Positive
Not working for JCBSonia
The link is broken now.Periosteum
Here's a regular expression for unionpay: (^62)([^\s]{14,17})Stratify
S
3

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://mcmap.net/q/64274/-how-do-you-detect-credit-card-type-based-on-number

Spherical answered 11/9, 2014 at 20:46 Comment(0)
A
2

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.

Advisement answered 6/5, 2011 at 12:27 Comment(1)
+1 Every now and then they also drop ranges, or mark them as being ATM only/no ecomm/no CNP etcCommute
C
1

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

Commute answered 6/5, 2011 at 12:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.