本文介绍了适用于java的信用卡验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要进行信用卡号验证。

I need to do a Credit card number validation.

当我用Google搜索时,我找到了 org.apache.commons.validator.CreditCardValidator 。但似乎它无法正常工作。
当我传递一个非数字字符时,它也是真的。

When I googled this I found the org.apache.commons.validator.CreditCardValidator. But seems like it is not working correctly.When I pass a non-digit character also it porvides true.

Apache的代码:

Code for Apache CreditCardValidator:

String ccNumber = "378282246310005";
CreditCardValidator creditCardValidator = new CreditCardValidator();
if(!creditCardValidator.isValid(ccNumber)) throw new Exception("Credit Card Number is not a valid one!");

然后,我根据卡片类型和卡号编写了以下方法来验证信用卡号码(使用luhn算法)。

Then, I wrote following methods to validate credit card numbers based on the card type and the card number (using the luhn's algorithm).

CardType验证器(如果卡片类型无效,则为null)

public String getCCType(String ccNumber){

        String visaRegex        = "^4[0-9]{12}(?:[0-9]{3})?$";
        String masterRegex      = "^5[1-5][0-9]{14}$";
        String amexRegex        = "^3[47][0-9]{13}$";
        String dinersClubrRegex = "^3(?:0[0-5]|[68][0-9])[0-9]{11}$";
        String discoverRegex    = "^6(?:011|5[0-9]{2})[0-9]{12}$";
        String jcbRegex         = "^(?:2131|1800|35\\d{3})\\d{11}$";
        String commonRegex      = "^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11})$";

        try {
            ccNumber = ccNumber.replaceAll("\\D", "");
            return (ccNumber.matches(visaRegex) ? "VISA" : ccNumber.matches(masterRegex) ? "MASTER" :ccNumber.matches(amexRegex) ? "AMEX" :ccNumber.matches(dinersClubrRegex) ? "DINER" :ccNumber.matches(discoverRegex) ? "DISCOVER"  :ccNumber.matches(jcbRegex) ? "JCB":null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

使用Luhn's algorithem的CardNumber验证器。

public boolean isValidCardNumber(String ccNumber){

        try {
            ccNumber = ccNumber.replaceAll("\\D", "");
            char[]      ccNumberArry    = ccNumber.toCharArray();

            int         checkSum        = 0;
            for(int i = ccNumberArry.length - 1; i >= 0; i--){

                char            ccDigit     = ccNumberArry[i];

                if((ccNumberArry.length - i) % 2 == 0){
                    int doubleddDigit = Character.getNumericValue(ccDigit) * 2;
                    checkSum    += (doubleddDigit % 9 == 0 && doubleddDigit != 0) ? 9 : doubleddDigit % 9;

                }else{
                    checkSum    += Character.getNumericValue(ccDigit);
                }

            }

            return (checkSum != 0 && checkSum % 10 == 0);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return false;
    }

我想知道,


  1. 除了org.apache之外,是否还有其他第三方来验证信用卡

  2. 是否有任何问题用我的代码?
    (我测试了好几次。到目前为止一切都那么好。我只想知道
    ,如果你能找到我没有发现的东西。)

参考文献:

推荐答案

您可以找到自定义植入这里的信用卡验证器正在进行信用卡号验证和信用卡类型检测,

You can find custom implantation of credit card validator here which is doing both credit card number validation plus credit card type detection,

这篇关于适用于java的信用卡验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 00:33