String Error CPF Android

Asked

Viewed 537 times

1

Does anyone have a CPF code for validation on Android? I have the following code:

public static boolean validateNotNull(View pView, String pMessage) {

     if (pView instanceof EditText) {
       EditText edText = (EditText) pView;
       Editable text = edText.getText();
       if (text != null) {
        String strText = text.toString();
        if (!TextUtils.isEmpty(strText)) {
         return true;
        }
       }
       // em qualquer outra condição é gerado um erro
       edText.setError(pMessage);
       edText.setFocusable(true);
       edText.requestFocus();
       return false;
      }
      return false;
     }

    public static boolean validateCPF(String CPF) {
         CPF = Mask.unmask(CPF);
         if (CPF.equals("00000000000") || CPF.equals("11111111111")
         || CPF.equals("22222222222") || CPF.equals("33333333333")
         || CPF.equals("44444444444") || CPF.equals("55555555555")
         || CPF.equals("66666666666") || CPF.equals("77777777777")
         || CPF.equals("88888888888") || CPF.equals("99999999999")) {
             return false;
         }
         char dig10, dig11;
         int sm, i, r, num, peso;
         try {
             sm = 0;
             peso = 10;
             for (i = 0; i < 9; i++) {
                 num = (int) (CPF.charAt(i) - 48);
                 sm = sm + (num * peso);
                 peso = peso - 1;
             }
             r = 11 - (sm % 11);
             if ((r == 10) || (r == 11))
                 dig10 = '0';
         else
             dig10 = (char) (r + 48);
         sm = 0;
         peso = 11;
         for (i = 0; i < 10; i++) {
             num = (int) (CPF.charAt(i) - 48);
            sm = sm + (num * peso);
            peso = peso - 1;
         }
         r = 11 - (sm % 11);
         if ((r == 10) || (r == 11))
             dig11 = '0';
             else
                 dig11 = (char) (r + 48);
             if ((dig10 == CPF.charAt(9)) && (dig11 == CPF.charAt(10)))
                 return (true);
             else
                 return (false);
         } catch (Exception erro) {
             return (false);
         }
    }

    public final static boolean validateEmail(String txtEmail) {
        if (TextUtils.isEmpty(txtEmail)) {
            return false;
        } else {
            return android.util.Patterns.EMAIL_ADDRESS.matcher(txtEmail).matches();
        }
    }

The same returns me a String error when the information is wrong, or when I try to delete a text:

06-19 13:28:18.922: E/Androidruntime(529): java.lang.Indexoutofboundsexception: setSpan (0 ... 1) ends Beyond length 0

  • 1

    java.lang.Indexoutofboundsexception, you are probably accessing position that does not exist.

  • But where would be the error in the code? I tried to solve and could not

  • Who is this setSpan? I haven’t tested your code yet.

1 answer

1

Put this at the beginning of replaceAll,you are probably trying to access an INDEX in the string that does not exist.

public static boolean validateCPF(String CPF) {

CPF = CPF.replaceAll(".","").replaceAll("-","");

if(CPF.length() < 11)
    return false;
}
  • I would delete the rest of the code?

  • to place the replaceALL?

  • No need to remove all the code. I just didn’t want to copy. to take out THE DOTS and DASHES. e -. for a CPF with no special character, only numbers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.