如何根据号码检测信用卡类型?
我试图弄清楚如何纯粹根据信用卡号来检测信用卡的类型。 有谁知道找到这个的明确、可靠的方法吗?
I'm trying to figure out how to detect the type of credit card based purely on its number. Does anyone know of a definitive, reliable way to find this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(29)
信用卡/借记卡号称为PAN,或主帐号。 PAN 的前六位数字取自属于发行银行的IIN 或发行人识别号(IIN 以前称为 BIN - 银行识别号 - 因此您可能会在某些文档中看到对该术语的引用)。 这六位数字受国际标准 ISO/IEC 7812 的约束,并且可以使用从号码判断卡的类型。
遗憾的是,实际的 ISO/IEC 7812 数据库并未公开提供,但有商业和免费的非官方列表,包括 在维基百科上。
无论如何,要从数字中检测类型,您可以使用如下正则表达式: Credit for原始表达式
Visa:
^4[0-9]{6,}$
Visa 卡号以 4 开头。MasterCard:
^5[1-5][0-9]{5,}|222[1-9][0-9]{3,}|22[3-9][0-9]{4 ,}|2[3-6][0-9]{5,}|27[01][0-9]{4,}|2720[0-9]{3,}$
2016 年之前, MasterCard 号码以数字 51 到 55 开头,但这只会检测 MasterCard 信用卡; 使用万事达卡系统发行的其他卡不属于此 IIN 范围。 2016 年,他们将添加 (222100-272099) 范围内的数字。American Express:
^3[47][0-9]{5,}$
American Express 卡号以 34 或 37 开头。Diners Club:
^3(?:0[0-5]|[68][0-9])[0-9]{4,}$
Diners Club 卡号以 300 到 305 开头, 36 或 38。有些 Diners Club 卡以 5 开头,有 16 位数字。 这些是 Diners Club 和万事达卡之间的合资企业,应像万事达卡一样进行处理。发现:
^6(?:011|5[0-9]{2})[0-9]{3,}$
发现卡号以 6011 或65.JCB:
^(?:2131|1800|35[0-9]{3})[0-9]{3,}$
JCB 卡开头为2131、1800 或 35。不幸的是,MasterCard 系统处理的许多卡类型不在 MasterCard 的 IIN 范围内(编号从 51...55 开始); 最重要的例子是 Maestro 卡,其中许多卡是从其他银行的 IIN 范围发行的,因此遍布整个号码空间。 因此,最好假设任何非您接受的其他类型的卡都必须是万事达卡。
重要:卡号的长度确实有所不同; 例如,Visa 过去发行过 13 位 PAN 的卡和 16 位 PAN 的卡。 Visa 的文件目前表明它可能会签发或可能已经签发了 12 到 19 位数字的号码。 因此,您不应检查卡号的长度,除非验证卡号至少有 7 位(对于完整的 IIN 加上一位校验位,应与 Luhn 算法)。
进一步提示:在处理持卡人 PAN 之前,从输入中去除所有空格和标点符号。 为什么? 因为通常更容易按组输入数字,类似于它们在实际信用卡正面的显示方式,即
比正确输入要容易得多
。用户,因为他们输入了您不期望的字符。
这还意味着确保您的输入字段有空间至少 24 个字符,否则输入空格的用户将耗尽空间。我建议您将字段宽度足以显示 32 个字符,最多允许 64 个; 这为扩展提供了充足的空间。
下面的图片可以提供更多见解:
更新 (2016): 万事达卡将从 开始实施新的 BIN 范围付款方式。
The credit/debit card number is referred to as a PAN, or Primary Account Number. The first six digits of the PAN are taken from the IIN, or Issuer Identification Number, belonging to the issuing bank (IINs were previously known as BIN — Bank Identification Numbers — so you may see references to that terminology in some documents). These six digits are subject to an international standard, ISO/IEC 7812, and can be used to determine the type of card from the number.
Unfortunately the actual ISO/IEC 7812 database is not publicly available, however, there are unofficial lists, both commercial and free, including on Wikipedia.
Anyway, to detect the type from the number, you can use a regular expression like the ones below: Credit for original expressions
Visa:
^4[0-9]{6,}$
Visa card numbers start with a 4.MasterCard:
^5[1-5][0-9]{5,}|222[1-9][0-9]{3,}|22[3-9][0-9]{4,}|2[3-6][0-9]{5,}|27[01][0-9]{4,}|2720[0-9]{3,}$
Before 2016, MasterCard numbers start with the numbers 51 through 55, but this will only detect MasterCard credit cards; there are other cards issued using the MasterCard system that do not fall into this IIN range. In 2016, they will add numbers in the range (222100-272099).American Express:
^3[47][0-9]{5,}$
American Express card numbers start with 34 or 37.Diners Club:
^3(?:0[0-5]|[68][0-9])[0-9]{4,}$
Diners Club card numbers begin with 300 through 305, 36 or 38. There are Diners Club cards that begin with 5 and have 16 digits. These are a joint venture between Diners Club and MasterCard and should be processed like a MasterCard.Discover:
^6(?:011|5[0-9]{2})[0-9]{3,}$
Discover card numbers begin with 6011 or 65.JCB:
^(?:2131|1800|35[0-9]{3})[0-9]{3,}$
JCB cards begin with 2131, 1800 or 35.Unfortunately, there are a number of card types processed with the MasterCard system that do not live in MasterCard’s IIN range (numbers starting 51...55); the most important case is that of Maestro cards, many of which have been issued from other banks’ IIN ranges and so are located all over the number space. As a result, it may be best to assume that any card that is not of some other type you accept must be a MasterCard.
Important: card numbers do vary in length; for instance, Visa has in the past issued cards with 13 digit PANs and cards with 16 digit PANs. Visa’s documentation currently indicates that it may issue or may have issued numbers with between 12 and 19 digits. Therefore, you should not check the length of the card number, other than to verify that it has at least 7 digits (for a complete IIN plus one check digit, which should match the value predicted by the Luhn algorithm).
One further hint: before processing a cardholder PAN, strip any whitespace and punctuation characters from the input. Why? Because it’s typically much easier to enter the digits in groups, similar to how they’re displayed on the front of an actual credit card, i.e.
is much easier to enter correctly than
There’s really no benefit in chastising the user because they’ve entered characters you don't expect here.
This also implies making sure that your entry fields have room for at least 24 characters, otherwise users who enter spaces will run out of room. I’d recommend that you make the field wide enough to display 32 characters and allow up to 64; that gives plenty of headroom for expansion.
Here's an image that gives a little more insight:
UPDATE (2016): Mastercard is to implement new BIN ranges starting Ach Payment.
在 javascript 中:
单元测试:
In javascript:
Unit test:
更新日期:2016 年 6 月 15 日(作为当前的最终解决方案)
请注意,我什至对投票最高的一个进行了投票,但为了清楚起见,这些正则表达式实际上有效,我对数千个进行了测试真实的 BIN 代码。 最重要的是使用起始字符串(^),否则在现实世界中会给出错误的结果!
JCB
^(?:2131|1800|35)[ 0-9]{0,}$
开头:2131, 1800, 35 (3528-3589)美国运通
^3[47] [0-9]{0,}$
开头:34, 37Diners Club
^3(?:0[0-59] {1}|[689])[0-9]{0,}$
开头:300-305、309、36、38-39签证
^4[0-9]{0,}$
开头:4MasterCard
^(5[1-5] |222[1-9]|22[3-9]|2[3-6]|27[01]|2720)[0-9]{0,}$
开头:2221 -2720, 51-55Maestro
^(5[06789]|6)[0-9]{0,}$
Maestro 始终在该范围内增长:60-69,以 / 开头,不是别的,但开头 5 无论如何都必须编码为 mastercard。 Maestro 卡必须在代码末尾被检测到,因为其他一些卡的范围在 60-69 之间。 请看一下代码。发现
^(6011|65|64[4-9]|62212[6-9]|6221[3-9]|622[2-8]|6229[01]| 62292[0-5])[0-9]{0,}$
发现编码相当困难,从以下内容开始:6011, 622126-622925, 644-649, 65In < strong>javascript 我使用这个函数。 当您将其分配给 onkeyup 事件并尽快给出结果时,这很好。
您可以在这里使用它:
http://jsfiddle.net/upN3L/69/
对于 PHP 使用此函数,这也会检测到一些子 VISA/MC 卡:
Updated: 15th June 2016 (as an ultimate solution currently)
Please note that I even give vote up for the one is top voted, but to make it clear these are the regexps actually works i tested it with thousands of real BIN codes. The most important is to use start strings (^) otherwise it will give false results in real world!
JCB
^(?:2131|1800|35)[0-9]{0,}$
Start with: 2131, 1800, 35 (3528-3589)American Express
^3[47][0-9]{0,}$
Start with: 34, 37Diners Club
^3(?:0[0-59]{1}|[689])[0-9]{0,}$
Start with: 300-305, 309, 36, 38-39Visa
^4[0-9]{0,}$
Start with: 4MasterCard
^(5[1-5]|222[1-9]|22[3-9]|2[3-6]|27[01]|2720)[0-9]{0,}$
Start with: 2221-2720, 51-55Maestro
^(5[06789]|6)[0-9]{0,}$
Maestro always growing in the range: 60-69, started with / not something else, but starting 5 must be encoded as mastercard anyway. Maestro cards must be detected in the end of the code because some others has in the range of 60-69. Please look at the code.Discover
^(6011|65|64[4-9]|62212[6-9]|6221[3-9]|622[2-8]|6229[01]|62292[0-5])[0-9]{0,}$
Discover quite difficult to code, start with: 6011, 622126-622925, 644-649, 65In javascript I use this function. This is good when u assign it to an onkeyup event and it give result as soon as possible.
Here you can play with it:
http://jsfiddle.net/upN3L/69/
For PHP use this function, this detects some sub VISA/MC cards too:
这是使用 Regex 检查信用卡类型的函数,c#
Here is the function to check Credit card type using Regex , c#
看看这个:
http://www.writingpar.com/bkp/home。 nsf/0/87256B280015193F87256CC70060A01B
Check this out:
http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CC70060A01B
最近我需要这样的功能,我正在将 Zend Framework Credit Card Validator 移植到 ruby。
红宝石宝石: https://github.com/Fivell/credit_card_validations
zend 框架: https://github.com/zendframework /zf2/blob/master/library/Zend/Validator/CreditCard.php
它们都使用 INN 范围来检测类型。 在这里你可以阅读关于INN
根据这个你可以检测信用卡(没有正则表达式,但是声明一些关于前缀和可能长度的规则)
所以我们对最常用的卡有下一个规则
然后通过搜索前缀和比较长度您可以检测信用卡品牌。 另外不要忘记 luhn 算法(这里有描述 http://en.wikipedia.org/wiki /卢恩)。
可以在此处找到更新
的规则列表 https://raw .githubusercontent.com/Fivell/credit_card_validations/master/lib/data/brands.yaml
recently I needed such functionality, I was porting Zend Framework Credit Card Validator to ruby.
ruby gem: https://github.com/Fivell/credit_card_validations
zend framework: https://github.com/zendframework/zf2/blob/master/library/Zend/Validator/CreditCard.php
They both use INN ranges for detecting type. Here you can read about INN
According to this you can detect credit card alternatively (without regexps,but declaring some rules about prefixes and possible length)
So we have next rules for most used cards
Then by searching prefix and comparing length you can detect credit card brand. Also don't forget about luhn algoritm (it is descibed here http://en.wikipedia.org/wiki/Luhn).
UPDATE
updated list of rules can be found here https://raw.githubusercontent.com/Fivell/credit_card_validations/master/lib/data/brands.yaml
这里是 所有与 CC 相关的内容的完整 C# 或 VB 代码 在代码项目上。
这篇文章已经发表了几年,没有负面评论。
Here's Complete C# or VB code for all kinds of CC related things on codeproject.
This article has been up for a couple years with no negative comments.
Anatoliy 在 PHP 中的回答:
Anatoliy's answer in PHP:
紧凑的 JavaScript 版本
Compact javascript version
在卡片范围识别 (CRR) 中,使用一系列正则表达式或其他硬编码范围的算法的一个缺点是,根据我的经验,BIN/IIN 确实会随着时间而变化。 卡片的联合品牌是一个持续存在的复杂问题。 不同的卡收单机构/商户可能需要您以不同的方式对待同一张卡,具体取决于地理位置等。
此外,在过去几年中,随着银联卡等的广泛流通,现有模型无法应对新的范围,这些范围有时会与它们所取代的更广泛的范围交织在一起。
了解您的系统需要覆盖的地理位置可能会有所帮助,因为某些范围仅限于特定国家/地区使用。 例如,范围 62 包括美国的一些 AAA 子范围,但如果您的商户基地位于美国境外,您也许可以将所有 62 视为银联。
您可能还会被要求根据商户位置对卡进行不同的处理。 例如,将某些英国卡在国内视为借记卡,但在国际上视为信用卡。
一家主要收单银行维护了一套非常有用的规则。 例如 https://www.barclaycard.co.uk/business /files/BIN-Rules-EIRE.pdf 和 https://www.barclaycard.co.uk/business/files/BIN-Rules-UK.pdf。 (截至 2017 年 6 月的有效链接,感谢提供更新参考链接的用户。)但请注意,虽然这些 CRR 规则可能代表发卡领域,因为它适用于该实体收购的商户,它不包括例如标识为 CUP/UPI 的范围。
这些注释适用于磁条 (MagStripe) 或 PKE (Pan Key Entry) 场景。 ICC/EMV 领域的情况又有所不同。
更新:此页面(以及链接的维基百科页面)上的其他答案的 JCB 一如既往地为 16 长。 然而,在我的公司,我们有一支专门的工程师团队,负责在多个收单银行和地区对我们的 POS 设备和软件进行认证。 该团队从 JCB 获得的最新认证卡包有一个 19 长 PAN 的通行证盒。
In Card Range Recognition (CRR), a drawback with algorithms that use a series of regex or other hard-coded ranges, is that the BINs/IINs do change over time in my experience. The co-branding of cards is an ongoing complication. Different Card Acquirers / merchants may need you treat the same card differently, depending on e.g. geolocation.
Additionally, in the last few years with e.g. UnionPay cards in wider circulation, existing models do not cope with new ranges that sometimes interleave with broader ranges that they supersede.
Knowing the geography your system needs to cover may help, as some ranges are restricted to use in particular countries. For example, ranges 62 include some AAA sub-ranges in the US, but if your merchant base is outside the US, you may be able to treat all 62 as UnionPay.
You may be also asked to treat a card differently based on merchant location. E.g. to treat certain UK cards as debit domestically, but as credit internationally.
There are very useful set of rules maintained by one major Acquiring Bank. E.g. https://www.barclaycard.co.uk/business/files/BIN-Rules-EIRE.pdf and https://www.barclaycard.co.uk/business/files/BIN-Rules-UK.pdf. (Valid links as of June 2017, thanks to the user who provided a link to updated reference.) But be aware of the caveat that, while these CRR rules may represent the Card Issuing universe as it applies to the merchants acquired by that entity, it does not include e.g. ranges identified as CUP/UPI.
These comments apply to magnetic stripe (MagStripe) or PKE (Pan Key Entry) scenarios. The situation is different again in the ICC/EMV world.
Update: Other answers on this page (and also the linked WikiPedia page) have JCB as always 16 long. However, in my company we have a dedicated team of engineers who certify our POS devices and software across multiple acquiring banks and geographies. The most recent Certification Pack of cards this team have from JCB, had a pass case for a 19 long PAN.
这是一个 php 类函数,按 CCnumber 返回 CCtype。
此代码不验证卡或不运行 Luhn 算法,仅尝试根据 此页面。 基本上使用CCnumber长度和CCcard前缀来确定CCcard类型。
Here is a php class function returns CCtype by CCnumber.
This code not validates the card or not runs Luhn algorithm only try to find credit card type based on table in this page. basicly uses CCnumber length and CCcard prefix to determine CCcard type.
信用卡的前几个数字可用于估计供应商:
The first numbers of the credit card can be used to approximate the vendor:
不要尝试在处理付款时检测信用卡类型。 您面临着拒绝有效交易的风险。
如果您需要向支付处理方提供信息(例如 PayPal 信用卡对象需要命名 卡类型),然后从最少的可用信息中猜测它,例如
此实现(仅使用前两位数字)足以识别所有主要的(在 PayPal 的情况下是所有受支持的)卡计划。 事实上,您可能想完全跳过例外并默认使用最流行的卡类型。 让支付网关/处理器告诉您在响应您的请求时是否存在验证错误。
现实情况是,您的支付网关并不关心您提供的价值。
Do not try to detect credit card type as part of processing a payment. You are risking of declining valid transactions.
If you need to provide information to your payment processor (e.g. PayPal credit card object requires to name the card type), then guess it from the least information available, e.g.
This implementation (using only the first two digits) is enough to identify all of the major (and in PayPal's case all of the supported) card schemes. In fact, you might want to skip the exception altogether and default to the most popular card type. Let the payment gateway/processor tell you if there is a validation error in response to your request.
The reality is that your payment gateway does not care about the value you provide.
Usman Y 的答案的 Swift 2.1 版本。
使用 print 语句通过某个字符串值验证 so 调用
Swift 2.1 Version of Usman Y's answer.
Use a print statement to verify so call by some string value
Stripe 提供了这个出色的 javascript 库用于卡方案检测。 让我添加一些代码片段并向您展示如何使用它。
首先将其添加到您的网页中,然后
使用函数cardType 来检测卡方案。
以下是更多示例和演示的参考链接。
Stripe has provided this fantastic javascript library for card scheme detection. Let me add few code snippets and show you how to use it.
Firstly Include it to your web page as
Secondly use the function cardType for detecting the card scheme.
Here are the reference links for more examples and demos.
您可以快速创建一个枚举来检测信用卡类型。
调用 CreditCardType.cardTypeForCreditCardNumber("#card number") 方法,该方法返回 CreditCardType 枚举值。
In swift you can create an enum to detect the credit card type.
Call the method CreditCardType.cardTypeForCreditCardNumber("#card number") which returns CreditCardType enum value.
@Anatoliy 答案的 javascript 改进
A javascript improve of @Anatoliy answer
我的 jQuery 解决方案:
如果返回 0,则未检测到信用卡类型。
应将“creditcard”类添加到信用卡输入字段中。
My solution with jQuery:
In case 0 is returned, credit card type is undetected.
"creditcard" class should be added to the credit card input field.
我搜索了很多信用卡格式和电话号码格式。 发现了很多好的技巧,但没有什么真正适合我的需求,所以我创建了这段代码。 你像这样使用它:
I searched around quite a bit for credit card formatting and phone number formatting. Found lots of good tips but nothing really suited my exact desires so I created this bit of code. You use it like this:
只需用勺子喂一点:
Just a little spoon feeding:
以下是用 Python 编写的一些布尔函数的示例,如果根据函数名称检测到卡,则返回 True。
Here is an example of some boolean functions written in Python that return
True
if the card is detected as per the function name.基于上述事实,我想保留一段JAVA代码来识别卡品牌。
参考
Based on the above facts I would like to keep a snippet of JAVA code to identify card brand.
Reference
在 kotlin 上试试这个。 添加正则表达式并添加到when语句中。
Try this for kotlin. Add Regex and add to the when statement.
分别与 匹配的正则表达式规则卡供应商:
(4\d{12}(?:\d{3})?)
对于 VISA。(5[1-5]\d{14})
对于万事达卡。(3[47]\d{13})
用于美国运通 (AMEX)。((?:5020|5038|6304|6579|6761)\d{12}(?:\d\d)?)
对于 Maestro。(3(?:0[0-5]|[68][0-9])[0-9]{11})
代表 Diners Club。(6(?:011|5[0-9]{2})[0-9]{12})
用于发现。(35[2-8][89]\d\d\d{10})
JCB。The regular expression rules that match the respective card vendors:
(4\d{12}(?:\d{3})?)
for VISA.(5[1-5]\d{14})
for MasterCard.(3[47]\d{13})
for AMEX.((?:5020|5038|6304|6579|6761)\d{12}(?:\d\d)?)
for Maestro.(3(?:0[0-5]|[68][0-9])[0-9]{11})
for Diners Club.(6(?:011|5[0-9]{2})[0-9]{12})
for Discover.(35[2-8][89]\d\d\d{10})
for JCB.另一个api解决方案在rapidapi 银行卡Bin Num Check有250K+ 发行卡类型。
只有一个 GET Rest api 请求并获取发卡机构信息,例如:
Another api solution at rapidapi Bank Card Bin Num Check there are 250K+ issued card type.
Only one GET rest api request and get card issuer info like:
基于该组规则: https://raw.githubusercontent Fivell 在这篇文章中提供的 .com/Fivell/credit_card_validations/master/lib/data/brands.yaml 我创建了该 php 方法:
Based on that set of rules : https://raw.githubusercontent.com/Fivell/credit_card_validations/master/lib/data/brands.yaml provided in this post by Fivell I created that php method :