简体   繁体   English

LUHN信用卡验证失败的有效卡号

[英]LUHN credit-card validation fails on a valid card number

In my application i want to check whether the user have entered valid card number for that i have used LUHN algorithm.I have created it as method and called in the mainactivity. 在我的应用程序中,我想检查用户是否输入了我使用LUHN算法的有效卡号。我已将其创建为方法并在mainactivity中进行了调用。 But even if i give valid card number it shows invalid.While entering card number i have given spaces in between i didn't know because of that its not validating properly. 但是即使我提供了有效的卡号,它也显示为无效。虽然输入卡号时我之间没有空格,但由于无法正确验证,所以我不知道。 Please help me in finding the mistake. 请帮助我发现错误。

CreditcardValidation.java

public class CreditcardValidation {
    String creditcard_validation,msg;
    //String mobilepattern;
     public static boolean isValid(long number) {

            int total = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);
            if ((total % 10 == 0) && (prefixMatched(number, 1) == true) && (getSize(number)>=16 ) && (getSize(number)<=19 )) {
                return true;
            } else {
                return false;
            }
        }

        public static int getDigit(int number) {

            if (number <= 9) {
                return number;
            } else {
                int firstDigit = number % 10;
                int secondDigit = (int) (number / 10);

                return firstDigit + secondDigit;
            }
        }
        public static int sumOfOddPlace(long number) {
            int result = 0;

            while (number > 0) {
                result += (int) (number % 10);
                number = number / 100;
            }

            return result;
        }

        public static int sumOfDoubleEvenPlace(long number) {

            int result = 0;
            long temp = 0;

            while (number > 0) {
                temp = number % 100;
                result += getDigit((int) (temp / 10) * 2);
                number = number / 100;
            }

            return result;
        }

        public static boolean prefixMatched(long number, int d) {

            if ((getPrefix(number, d) == 5)
                    || (getPrefix(number, d) == 4)
                    || (getPrefix(number, d) == 3)) {

                if (getPrefix(number, d) == 4) {
                    System.out.println("\nVisa Card ");
                } else if (getPrefix(number, d) == 5) {
                    System.out.println("\nMaster Card ");
                } else if (getPrefix(number, d) == 3) {
                    System.out.println("\nAmerican Express Card ");
                }

                return true;

            } else {

                return false;

            }
        }

        public static int getSize(long d) {

            int count = 0;

            while (d > 0) {
                d = d / 10;

                count++;
            }

            return count;

        }

        public static long getPrefix(long number, int k) {

            if (getSize(number) < k) {
                return number;
            } else {

                int size = (int) getSize(number);

                for (int i = 0; i < (size - k); i++) {
                    number = number / 10;
                }

                return number;

            }

        }


        public String creditcardvalidation(String creditcard)
        {       
             Scanner sc = new Scanner(System.in);

              this.creditcard_validation= creditcard;
              long input = 0;
             input = sc.nextLong();
            //long input = sc.nextLong();
               if (isValid(input) == true) {
                   Log.d("Please fill all the column","valid");
                msg="Valid card number";

               }
               else{
                   Log.d("Please fill all the column","invalid");
                msg="Please enter the valid card number";

               }

               return msg;
} 
} 


MainActivity.java

addcard.setOnClickListener(new OnClickListener() 
{   
    @Override
    public void onClick(View v) {


            if(v.getId()==R.id.btn_add)
            {
                creditcard= card_number.getText().toString();
                cv = new  CreditcardValidation();
                String mob = cv.creditcardvalidation(creditcard);
                 Toast.makeText(getActivity(), mob, 1000).show();``

refer code below 参考下面的代码

 EditText cardNumber=(EditText)findViewById(R.id.cardNumber);
        String CreditCardType = "Unknown";

       /// Remove all spaces and dashes from the passed string
        String CardNo ="9292304336";///////cardNumber.getText().toString();           
        CardNo = CardNo.replace(" ", "");//removing empty space
             CardNo = CardNo.replace("-", "");//removing '-'
              twoDigit=Integer.parseInt(CardNo.substring(0, 2));
                                 System.out.println("----------twoDigit--"+twoDigit);
              fourDigit=Integer.parseInt(CardNo.substring(0, 4));
                                 System.out.println("----------fourDigit--"+fourDigit);
             oneDigit=Integer.parseInt(Character.toString(CardNo.charAt(0)));
                                System.out.println("----------oneDigit--"+oneDigit);

             boolean cardValidation=false;
            // 'Check that the minimum length of the string isn't <14 characters and -is- numeric
             if(CardNo.length()>=14)
             {

                 cardValidation=cardValidationMethod(CardNo);

             }

 boolean cardValidationMethod(String CardNo)
     {
        //'Check the first two digits first,for AmericanExpress
        if(CardNo.length()==15 && (twoDigit==34 || twoDigit==37))
        return true;
        else
            //'Check the first two digits first,for MasterCard
            if(CardNo.length()==16 && twoDigit>=51 && twoDigit<=55)
                return true;
        else
            //'None of the above - so check the 'first four digits collectively
                if(CardNo.length()==16 && fourDigit==6011)//for DiscoverCard
                return true;
        else

            if(CardNo.length()==16 || CardNo.length()==13 && oneDigit==4)//for VISA
                return true;
            else
                return false;       
     } 

also u can refer this demo project 你也可以参考这个演示项目

Scanner.nextLong() will stop reading as spaces (or other non-digit characters) are encountered. 当遇到空格(或其他非数字字符)时, Scanner.nextLong()停止读取

For instance, if the input is 1234 567 .. then nextLong() will only read 1234 . 例如,如果输入为1234 567 ..那么nextLong()读取1234

However, while spaces in the credit-card will [likely] cause it to fail LUHN validation with the above code, I make no guarantee that removing the spaces would make it pass - I'd use a more robust (and well-tested) implementation from the start. 但是,尽管信用卡中的空格将[可能]导致上述代码无法通过LUHN验证,但我无法保证删除空格会使它通过-我会使用更可靠(经过充分测试)的方法从一开始就执行。 There is no need to rewrite such code. 无需重写此类代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM