从卡号识别卡类型

发布于 2024-11-05 15:01:47 字数 495 浏览 0 评论 0原文

我有一系列的卡类型,看起来像这样

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};

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野稚 2024-11-12 15:01:47

我相信您可能正在寻找这样的东西:

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

最初发布于此处@ http://mel-green.com/2008/11/define-credit-card-type-with-javascript/

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/

盛装女皇 2024-11-12 15:01:47

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

http://jquerycreditcardvalidator.com/

另请参阅此 StackOverflow 答案以获得优秀的答案关于信用卡号与卡类型之间关系的讨论:

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

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

以酷 2024-11-12 15:01:47

卡公司有一个相当明确的前缀列表,这些前缀特定于卡类型。

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

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

您可能还想查看维基百科:http://en.wikipedia.org/wiki/Credit_card_numbers

缺点是,虽然现在流通的前缀几乎是固定的,但他们总有可能需要要提出更多,因此您需要确保所有前缀列表保持最新,特别是当您使用它来检查较长的前缀范围时。

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.

与风相奔跑 2024-11-12 15:01:47

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文