信用卡验证器的Java

我需要做一个信用卡号码验证。

当我谷歌搜索时,我发现了org.apache.commons.validator.CreditCardValidator 。 但似乎它不能正常工作。 当我传递一个非数字字符时,它也是真实的。

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算法)编写了以下方法来验证信用卡号码。

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|35d{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|35d{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;
    }

CardNumber验证器使用Luhn的算法。

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

我想知道,

  • 除了org.apache之外,还有其他任何一个第三方会不会验证信用卡?
  • 我的代码有问题吗? (我测试了好几次,到目前为止都很好,我只是想知道你是否能找到我没有的东西。)
  • 参考文献:如何根据数字检测信用卡类型?


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

    http://www.esupu.com/credit-card-validator-java/


    我很久以前做过这件事,对不起,代码是在C.轻松转换。 希望这会帮助你。

    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    
    int CardNoChecker(unsigned long long int Number)
    {
            int dijts=0;
            int Ans=0;
            {
                unsigned long long int k=1;
                while(Number%k!=Number)
                {
                        dijts=dijts+1;
                        k=k*10;
                }
            }
        {
            int i=1;
            int Product=0;
            int Sum=0;
            for(i=dijts;i>=1;i--)
            {
                    if(i%2==0)
                    {
                        if((Number%10)*2<10)
                        Product = Product + (  Number % 10  ) * 2 ;
                        else
                        {
                            int No=(Number%10)*2;
                            Product = Product + No/10;
                            Product = Product + No%10;
                        }
                    }
                    else
                    {
                        Sum = Sum + (  Number  % 10     ) ;
                    }
                    Number=Number /10;
            }
            Ans = Sum + Product ;
        }
        if(Ans%10==0)
        return (1);
        else
        return (0);
    }
    
    int main()
    {
        unsigned long long int CardNO;
        int valid=0;
        while(!valid)
        {
            int CnV=0;
            int VC=0;
            int AE=0;
            int MC=0;
            printf("Enter Card NO : ");
            scanf("%llu",&CardNO);
            if(CardNO/1000000000000==4 || CardNO/1000000000000000==4)
            {
                VC=1;
            }
            else if(CardNO/10000000000000==34 ||CardNO/10000000000000==37)
            {
                AE=1;
            }
            else if(CardNO/100000000000000==51 || CardNO/100000000000000==52 || CardNO/100000000000000==53 || CardNO/100000000000000==54 || CardNO/100000000000000==55)
            {
                MC=1;
            }
            CnV=CardNoChecker(CardNO);
            if(CnV && MC )
             printf("This is a Valid Master Cardnn");
             else if(CnV && VC )
             printf("This is a Valid Visa Cardnn");
             else if(CnV && AE )
             printf("This is a Valid American Express Cardnn");
             else
            printf("Card is Not Valid'nn");
        }
    
        return (0);
    }
    
    链接地址: http://www.djcxy.com/p/70707.html

    上一篇: Credit Card validator for java

    下一篇: Regex for Discover credit card