Identify card type from card number

This question already has an answer here:

  • How do you detect Credit card type based on number? 26 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/


    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.


    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

    链接地址: http://www.djcxy.com/p/70700.html

    上一篇: 用于确定Android中信用卡的正则表达式

    下一篇: 从卡号中识别卡类型