Problemas de validação CPF

2 respostas
jsnpereira

Olá, pessoal!

Já tentei várias vezes e testamos o meu CPF mas apareceu o mensagem “Erro de digitação”. achei estranho. esse material que pesquisei no site e programei a validar cpf. veja os código em baixo.

Esse classe Servelts que recebe o número CPF e testar a validar cpf e dpois tornar verdadeira aparece outra pagina “Sucesso”.

public class ValidaCPF extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cpf = request.getParameter("cpf");
		
		Validacao valida = new Validacao();
		
               //esse metodo da classe validacao para testar CPF e se for verdadeira e retorna True no atributo estado.
		boolean estado = valida.calculaCPF(cpf);
		
                // comparar se for igual verdadeira e enviar a mensagem da tela HTML, caso nao for verdadeira e aparece a mensagem da caixa de dialogo e volta tela principal.
		if(estado == true){
			System.out.println(cpf);
			JOptionPane.showMessageDialog(null, "Digita com sucesso!");
			response.sendRedirect("sucesso.htm");
			
					
		} else {
			JOptionPane.showMessageDialog(null,"Favor digita correto CPF!!");
			response.sendRedirect("index.htm");
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}

Esse é classe da validação

public class Validacao {
public boolean calculaCPF(String cpfNum){
		int[]cpf = new int[cpfNum.length()];
		int resultP= 0;
		int resultS = 0;
		boolean estado = false;
		
		//Converte a string para um array de inteiro
		for(int i = 0; i<cpfNum.length(); i++){
			cpf[i] = Integer.parseInt(cpfNum.substring(i, i+1));
		}
		
		for (int i=0; i<9;i++){
			int cont = 10;
			resultP += cpf[i]*(cont--);
		}
		
		int divP= resultP % 11;
		
		if(divP == cpf[9]){
			estado = true;
		} else {	
			for(int i=0; i<10;i++){
				int cont = 11;
				resultS = cpf[i]* (cont--);
			}
			
			int divS= resultS % 11;
			
			if(divS == cpf[10]){
				estado = true;
			}
		}
		
		return estado;
	}
}

Espero que vcs me ajudem…

2 Respostas

viniciusalvess

Use BrasilUtils para validar ;

L

segue esse codigo...bem facilll

[color=red]valida o cpf[/color]

public class ValidaCPF {   


	public ValidaCPF() {   
	}   

	public static boolean validaCpf(String xCPF)   
	{   
		try  
		{   

			//Testa se o CPF é válido ou não   

			int d1,d4,xx,nCount,resto,digito1,digito2;   
			String Check;   
			String Separadores = "/-.";   
			d1 = 0; d4 = 0; xx = 1;   
			for (nCount = 0; nCount < xCPF.length() -2; nCount++)   
			{   
				String s_aux = xCPF.substring(nCount, nCount+1);   

				//System.out.println(s_aux);   

				if (Separadores.indexOf(s_aux) == -1) {   
					d1 = d1 + ( 11 - xx ) * Integer.valueOf (s_aux).intValue();   
					d4 = d4 + ( 12 - xx ) * Integer.valueOf (s_aux).intValue();   
					xx++;   
				};   
			};   
			resto = (d1 % 11);   
			if (resto < 2)   
			{   
				digito1 = 0;   
			}   
			else  
			{   
				digito1 = 11 - resto;   
			}   

			d4 = d4 + 2 * digito1;   
			resto = (d4 % 11);   
			if (resto < 2)   
			{   
				digito2 = 0;   
			}   
			else  
			{   
				digito2 = 11 - resto;   
			}   



			Check = String.valueOf(digito1) + String.valueOf(digito2);   

			String s_aux2 = xCPF.substring (xCPF.length()-2, xCPF.length());  

			//System.out.println(s_aux2);   
			//System.out.println(Check);   

			if (s_aux2.compareTo (Check) != 0)   
			{   

				return false;   
			}   
			return true;   
		}   
		catch (Exception e)   
		{   
			return false;   
		}   
	}   

}

[color=red]chamando a class valida cpf[/color]

private void btnValidarActionPerformed(java.awt.event.ActionEvent evt) {
    	
    	String xCpf = txtValidaCpf.getText();

		if (!ValidaCPF.validaCpf(xCpf)) {

			txtValidaCpf.setBackground(Color.RED);
			

			JOptionPane.showMessageDialog(null, "Digite o CPF Certo", "ERROR",
					JOptionPane.ERROR_MESSAGE);
			
			txtValidaCpf.setText(null);
 
		} else {
			txtValidaCpf.setBackground(Color.WHITE);

		}
	}
Criado 21 de dezembro de 2010
Ultima resposta 21 de dez. de 2010
Respostas 2
Participantes 3