Bean para validação de CPF/CNPJ

3 respostas
A

Gostaria de qquer tipo de ajuda para um Bean que faça esta validação. Se alguém puder ajudar, desde já agradeço.

i[]'s
Tico

3 Respostas

Mauricio_Linhares

Diretamente do GUJ:

https://brazilutils.dev.java.net/

betonit

Dá uma googlada q lhe trará 845945983453 results sobre.

dsiviotti

Coloque no seu bean:

public static boolean isValid(String cpfOrCnpj){
        if (cpfOrCnpj == null) return false;
	String n = cpfOrCnpj.replaceAll("[^0-9]*","");
	boolean isCnpj = n.length() == 14;
	boolean isCpf = n.length() == 11;
	if (!isCpf && !isCnpj) return false;
        int i; int j;   // just count 
        int digit;      // A number digit
        int coeficient; // A coeficient  
        int sum;        // The sum of (Digit * Coeficient)
	int[] foundDv = {0,0}; // The found Dv1 and Dv2
        int dv1 = Integer.parseInt(String.valueOf(n.charAt(n.length()-2)));
        int dv2 = Integer.parseInt(String.valueOf(n.charAt(n.length()-1)));       
        for (j = 0; j < 2; j++) {
            sum = 0;
            coeficient = 2;
            for (i = n.length() - 3 + j; i >= 0 ; i--){
                digit = Integer.parseInt(String.valueOf(n.charAt(i)));               
                sum += digit * coeficient;
                coeficient ++;
                if (coeficient > 9 && isCnpj) coeficient = 2;                
            }                
            foundDv[j] = 11 - sum % 11;
            if (foundDv[j] >= 10) foundDv[j] = 0;
        }
        return dv1 == foundDv[0] && dv2 == foundDv[1];
	}
Criado 22 de junho de 2005
Ultima resposta 23 de jun. de 2005
Respostas 3
Participantes 4