我想知道如何仅根据信用卡的号码来判断信用卡的类型。有谁知道确定可靠的方法吗?


当前回答

一个javascript改进的@Anatoliy答案

function getCardType (number) { const numberFormated = number.replace(/\D/g, '') var patterns = { VISA: /^4[0-9]{12}(?:[0-9]{3})?$/, MASTER: /^5[1-5][0-9]{14}$/, AMEX: /^3[47][0-9]{13}$/, ELO: /^((((636368)|(438935)|(504175)|(451416)|(636297))\d{0,10})|((5067)|(4576)|(4011))\d{0,12})$/, AURA: /^(5078\d{2})(\d{2})(\d{11})$/, JCB: /^(?:2131|1800|35\d{3})\d{11}$/, DINERS: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/, DISCOVERY: /^6(?:011|5[0-9]{2})[0-9]{12}$/, HIPERCARD: /^(606282\d{10}(\d{3})?)|(3841\d{15})$/, ELECTRON: /^(4026|417500|4405|4508|4844|4913|4917)\d+$/, MAESTRO: /^(5018|5020|5038|5612|5893|6304|6759|6761|6762|6763|0604|6390)\d+$/, DANKORT: /^(5019)\d+$/, INTERPAYMENT: /^(636)\d+$/, UNIONPAY: /^(62|88)\d+$/, } for (var key in patterns) { if (patterns[key].test(numberFormated)) { return key } } } console.log(getCardType("4539 5684 7526 2091"))

其他回答

一个javascript改进的@Anatoliy答案

function getCardType (number) { const numberFormated = number.replace(/\D/g, '') var patterns = { VISA: /^4[0-9]{12}(?:[0-9]{3})?$/, MASTER: /^5[1-5][0-9]{14}$/, AMEX: /^3[47][0-9]{13}$/, ELO: /^((((636368)|(438935)|(504175)|(451416)|(636297))\d{0,10})|((5067)|(4576)|(4011))\d{0,12})$/, AURA: /^(5078\d{2})(\d{2})(\d{11})$/, JCB: /^(?:2131|1800|35\d{3})\d{11}$/, DINERS: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/, DISCOVERY: /^6(?:011|5[0-9]{2})[0-9]{12}$/, HIPERCARD: /^(606282\d{10}(\d{3})?)|(3841\d{15})$/, ELECTRON: /^(4026|417500|4405|4508|4844|4913|4917)\d+$/, MAESTRO: /^(5018|5020|5038|5612|5893|6304|6759|6761|6762|6763|0604|6390)\d+$/, DANKORT: /^(5019)\d+$/, INTERPAYMENT: /^(636)\d+$/, UNIONPAY: /^(62|88)\d+$/, } for (var key in patterns) { if (patterns[key].test(numberFormated)) { return key } } } console.log(getCardType("4539 5684 7526 2091"))

匹配对应卡供应商的正则表达式规则:

(4\d{12}(?:\d{3})?) (5[1-5]\d{14}) (3[47]\d{13})为美国运通。 ((?: 5020 | 5038 | 6304 | 6579 | 6761) \ d {12} (?: \ \ d) ?)大师。 (3(?:0[0-5]|[68][0-9])[0-9]{11}) (6(?:011|5[0-9]{2})[0-9]{12})用于发现。 (35[2-8][89]\d\d\d{10})

public string GetCreditCardType(string CreditCardNumber)
{
    Regex regVisa = new Regex("^4[0-9]{12}(?:[0-9]{3})?$");
    Regex regMaster = new Regex("^5[1-5][0-9]{14}$");
    Regex regExpress = new Regex("^3[47][0-9]{13}$");
    Regex regDiners = new Regex("^3(?:0[0-5]|[68][0-9])[0-9]{11}$");
    Regex regDiscover = new Regex("^6(?:011|5[0-9]{2})[0-9]{12}$");
    Regex regJCB = new Regex("^(?:2131|1800|35\\d{3})\\d{11}$");


    if (regVisa.IsMatch(CreditCardNumber))
        return "VISA";
    else if (regMaster.IsMatch(CreditCardNumber))
        return "MASTER";
    else  if (regExpress.IsMatch(CreditCardNumber))
        return "AEXPRESS";
    else if (regDiners.IsMatch(CreditCardNumber))
        return "DINERS";
    else if (regDiscover.IsMatch(CreditCardNumber))
        return "DISCOVERS";
    else if (regJCB.IsMatch(CreditCardNumber))
        return "JCB";
    else
        return "invalid";
}

下面是使用Regex, c#检查信用卡类型的函数

Usman Y回答的Swift 2.1版本。 使用print语句来验证,因此调用某个字符串值

print(self.validateCardType(self.creditCardField.text!))

func validateCardType(testCard: String) -> String {

    let regVisa = "^4[0-9]{12}(?:[0-9]{3})?$"
    let regMaster = "^5[1-5][0-9]{14}$"
    let regExpress = "^3[47][0-9]{13}$"
    let regDiners = "^3(?:0[0-5]|[68][0-9])[0-9]{11}$"
    let regDiscover = "^6(?:011|5[0-9]{2})[0-9]{12}$"
    let regJCB = "^(?:2131|1800|35\\d{3})\\d{11}$"


    let regVisaTest = NSPredicate(format: "SELF MATCHES %@", regVisa)
    let regMasterTest = NSPredicate(format: "SELF MATCHES %@", regMaster)
    let regExpressTest = NSPredicate(format: "SELF MATCHES %@", regExpress)
    let regDinersTest = NSPredicate(format: "SELF MATCHES %@", regDiners)
    let regDiscoverTest = NSPredicate(format: "SELF MATCHES %@", regDiscover)
    let regJCBTest = NSPredicate(format: "SELF MATCHES %@", regJCB)


    if regVisaTest.evaluateWithObject(testCard){
        return "Visa"
    }
    else if regMasterTest.evaluateWithObject(testCard){
        return "MasterCard"
    }

    else if regExpressTest.evaluateWithObject(testCard){
        return "American Express"
    }

    else if regDinersTest.evaluateWithObject(testCard){
        return "Diners Club"
    }

    else if regDiscoverTest.evaluateWithObject(testCard){
        return "Discover"
    }

    else if regJCBTest.evaluateWithObject(testCard){
        return "JCB"
    }

    return ""

}

这里有完整的c#或VB代码,用于各种CC相关的东西。

IsValidNumber GetCardTypeFromNumber GetCardTestNumber PassesLuhnTest

这篇文章已经发布了几年,没有任何负面评论。