Error in CPF validation

Asked

Viewed 369 times

2

I’m trying to do a validation on CPF and I’m finding problems. When I type in any number that is not in if, it inserts into the seat normally, but when I type numbers from inside the if I’d like him to show the invalid Cpf message and not play for bank, but the most I could get was to insert "INVALID NUMBER!!" into the bank... If anyone can help me. I’m sending three classes Conexao, Socio and validarCpf.

Class Conexao :

public void CadastrarUsuario() {
    conectarBanco();

    CpfValidacao ver2 = new CpfValidacao();

    try {
        Socio cadastrarNovoSocio = new Socio();
        CpfValidacao ver = new CpfValidacao();

        String sql = ("insert into cadastrousuario values('"
                + cadastrarNovoSocio.getNome() + "','"
                + cadastrarNovoSocio.getEmail() + "','"
                + cadastrarNovoSocio.getDdd() + "','"
                + cadastrarNovoSocio.getTelefoneUsuario() + "','"
                + ver.getValidaCpf()+"')");

        stm.executeUpdate(sql);
        con.close();

    } catch (Exception e) {
        System.out.println("Erro: " + e);

    }
}

Classe Socio

public String getCpf() {
    System.out.println("Informe o CPF: ");
    cpf = scan.nextLine();
    return cpf;
}

public void setCpf(String cpf) {
    this.cpf = cpf;
}

Class Cpfvalidation

public class CpfValidacao {
    private String validaCpf;

    public void fecharConexao(){
        Conexao teste = new Conexao();
        teste.desconectarBanco();
    }
    public String getValidaCpf() {
        Socio validar = new Socio();
        validaCpf = validar.getCpf();

        if (validaCpf.equals("11111111111") || validaCpf.equals("22222222222")
                || validaCpf.equals("33333333333") || validaCpf.equals("44444444444")
                || validaCpf.equals("55555555555") ||validaCpf.equals("66666666666")
                || validaCpf.equals("77777777777") ||validaCpf.equals("88888888888")
                || validaCpf.equals("99999999999") ||validaCpf.length() != 11) {

            fecharConexao();
            return "CPF INVÁLIDO!!!";
        }
        return validaCpf;
    }
    public void fecharConexao(String validaCpf) {
        this.validaCpf = validaCpf;
    }
}
  • This question is duplicated: http://answall.com/questions/58349/d%C3%Bavida-com-valida%C3%A7%C3%A3o-Cpf (and this seems to be better formatted).

  • This validaCpf method does not take cases like 12345678910! From a look here : http://javafree.uol.com.br/topic-851371-Validacao-de-CPF.html

1 answer

1

You are not entering the return of the direct method in your database no matter what it returns, validate Cpf before giving an input in the table.

and a Voce observation could replace all this if

    if (validaCpf.equals("11111111111") || validaCpf.equals("22222222222")
            || validaCpf.equals("33333333333") || validaCpf.equals("44444444444")
            || validaCpf.equals("55555555555") ||validaCpf.equals("66666666666")
            || validaCpf.equals("77777777777") ||validaCpf.equals("88888888888")
            || validaCpf.equals("99999999999") ||validaCpf.length() != 11) {

        fecharConexao();
        return "CPF INVÁLIDO!!!";
    }

by a regular expression:

    if (!validaCpf.matches("\\d{11,11}")) {
       fecharConexao();
        return "CPF INVÁLIDO!!!";
      }

Browser other questions tagged

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