Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Utility class to validate CPF and CNPJ document types. For CPF use isValidSsn and for CNPJ use isValidTfn. Added to repo https://github.com/adrianoluis/misc-tools
public class DocumentUtil {
// CPF
private static final int[] WEIGHT_SSN = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2};
// CNPJ
private static final int[] WEIGHT_TFN = {6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2};
private static int sum(int[] weight, char[] numbers, int length) {
if (length <= 0) return 0;
final int nIdx = length - 1;
final int wIdx = weight.length > numbers.length ? length : nIdx;
return (sum(weight, numbers, nIdx) + Character.getNumericValue(numbers[nIdx]) * weight[wIdx]);
}
private static int calculate(final String document, final int[] weight) {
final char[] numbers = document.toCharArray();
int sum = sum(weight, numbers, numbers.length);
sum = 11 - (sum % 11);
return sum > 9 ? 0 : sum;
}
private static boolean check(String tfn, int length, int[] weight) {
final String number = tfn.substring(0, length);
final int digit1 = calculate(number, weight);
final int digit2 = calculate(number + digit1, weight);
return tfn.equals(number + digit1 + digit2);
}
/**
* Valida CPF
*/
public static boolean isValidSsn(String ssn) {
if (ssn == null || !ssn.matches("\\d{11}") || ssn.matches(ssn.charAt(0) + "{11}")) return false;
return check(ssn, 9, WEIGHT_SSN);
}
/**
* Valida CNPJ
*/
public static boolean isValidTfn(String tfn) {
if (tfn == null || !tfn.matches("\\d{14}")) return false;
return check(tfn, 12, WEIGHT_TFN);
}
}
@santosediego
Copy link

santosediego commented Aug 13, 2020

Muito obrigado Adriano!

@Romaomoura
Copy link

Romaomoura commented Aug 17, 2020

Valeu, ajudou demais!

@Pipiluco
Copy link

Pipiluco commented Aug 19, 2020

Obrigado

@nasseroth
Copy link

nasseroth commented Aug 21, 2020

Muito obrigado pela sua contribuição, ajudou de mais!

@qmclouca
Copy link

qmclouca commented Sep 6, 2020

Fazendo o curso de Nélio Alves, usando no curso do Spring. Parabéns!

@rasec23rj
Copy link

rasec23rj commented Sep 18, 2020

Muito bom Obg!

@wep1980
Copy link

wep1980 commented Oct 5, 2020

Nota 10!

@lucassverissimo
Copy link

lucassverissimo commented Oct 8, 2020

Valeu, cara. Ajudou muito!!

@LeonilSouza
Copy link

LeonilSouza commented Oct 9, 2020

Maravilha, obrigado. Deus abençoe.

@vanderson22
Copy link

vanderson22 commented Oct 13, 2020

Muito Obrigado!

@lucasfrancisco
Copy link

lucasfrancisco commented Oct 23, 2020

Obrigado por compartilhar, grande ajuda!

@MarinhoFeliphe
Copy link

MarinhoFeliphe commented Oct 27, 2020

Valeu mesmo!

@Higorfarias
Copy link

Higorfarias commented Nov 4, 2020

Thanks!

@eduardobatistadev
Copy link

eduardobatistadev commented Nov 9, 2020

Top, ajudou muito!

@mig-ramos
Copy link

mig-ramos commented Jan 21, 2021

Sensacional!

@soumusico2
Copy link

soumusico2 commented Feb 28, 2021

Obrigado!

@iangerolamo
Copy link

iangerolamo commented Mar 7, 2021

muito obrigado maestro

@gleds3000
Copy link

gleds3000 commented Mar 25, 2021

Muito bom, obrigado por compartilhar!

@williamartins02
Copy link

williamartins02 commented Apr 10, 2021

Obrigado Guerrero, me ajudou muito no projeto do Nélio Alves.

@RicardoTSilva2303
Copy link

RicardoTSilva2303 commented Apr 23, 2021

Muito obrigado Luis, me ajudou bastante.

@piresalexsandro
Copy link

piresalexsandro commented May 8, 2021

Obrigado!!!

@gogaoliveira
Copy link

gogaoliveira commented May 12, 2021

top

@angelosuporte
Copy link

angelosuporte commented Jun 7, 2021

Obrigado

@PortelaVictor
Copy link

PortelaVictor commented Jul 3, 2021

Obrigado Luis, ajudou bastante.

@jrsguitar
Copy link

jrsguitar commented Jul 13, 2021

Muito obrigado!

@Pedrolaires
Copy link

Pedrolaires commented Jan 14, 2022

Até hoje ajudando muita gente, obrigado!

@newtonneto
Copy link

newtonneto commented Jan 21, 2022

Muito obrigado

@charllysonsouza
Copy link

charllysonsouza commented Jan 22, 2022

Obrigado! Estou vindo pelo curso do Nélio da Udemy. Valeu!

@lissa-sonoda
Copy link

lissa-sonoda commented Mar 2, 2022

Muito obrigada, Luis!! Ajudou demais!!

@marland-tales
Copy link

marland-tales commented Apr 5, 2022

Tks bro!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment