Javascript 从卡号识别卡类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5911236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:28:33  来源:igfitidea点击:

Identify card type from card number

javascriptcredit-cardluhn

提问by Ghatzi

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.

然而,id 希望能够根据输入的卡号找到卡类型。例如,如果他们从下拉列表中选择他们的卡类型,即签证,那么信用卡号应该以 4 开头,否则当他们提交表格时,应该显示某种消息,说这是(无论是什么卡,请换卡类型)。任何帮助,将不胜感激。

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.

卡号文本字段的 id 是 CardNumber。我不确定可能需要什么其他信息,我有一个名为 Validate 的函数,它验证表单的其余部分,还有一个函数调用 Calculate,它执行 luhn 检查。

回答by Russ Clarke

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/

最初发布在这里@ http://mel-green.com/2008/11/determine-credit-card-type-with-javascript/

回答by Spudley

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.

这些前缀的范围从一个数字(所有以“5”开头的都是万事达卡)到一些长达六到七位数的字符串,用于更模糊的卡类型,也用于主流卡,但不仅能够识别卡类型也是开证行。

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

这是我找到的一个资源:http: //www.beachnet.com/~hstiles/cardtype.html

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

您可能还想查看维基百科: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.

不利的一面是,虽然现在流通的前缀几乎是固定的,但总有可能他们需要想出更多,因此您需要确保任何前缀列表都是最新的,尤其是当您使用它来检查较长的前缀范围时。

回答by Jon Schneider

Here's a jQuery plugin designed for exactly this:

这是一个专门为此设计的jQuery插件:

http://jquerycreditcardvalidator.com/

http://jquerycreditcardvalidator.com/

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

另请参阅此 StackOverflow 答案,以了解信用卡号与卡类型之间的关系:

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

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

回答by Alex K.

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).

卡前缀方案在 BIN(银行 ID 号)列表中详细说明并定期更改,我建议不要针对 PAN 的第 6 位完整数字进行验证,除非您计划定期更新并且仅对前几位数字进行粗略检查(例如,您缺少 48* 用于签证/选举,签证长度可以跨越 16-19)。

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

如果您在英国;http://www.barclaycard.co.uk/business/documents/pdfs/bin_rules.pdf