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 recursiveSum(int[] weight, char[] chr, int number) {
if (number <= 0) return 0;
final int chrIndex = number - 1;
final int weightIndex = weight.length > chr.length ? number : chrIndex;
return (recursiveSum(weight, chr, chrIndex) +
Character.getNumericValue(chr[chrIndex]) * weight[weightIndex]);
}
private static int calculate(final String str, final int[] weight) {
final char[] chr = str.toCharArray();
int sum = recursiveSum(weight, chr, chr.length);
sum = 11 - (sum % 11);
return sum > 9 ? 0 : sum;
}
private static boolean checkEquals(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
*
* @param ssn
* @return
*/
public static boolean isValidSsn(String ssn) {
if (ssn == null || !ssn.matches("\\d{11}") || ssn.matches(ssn.charAt(0) + "{11}")) return false;
return checkEquals(ssn, 9, WEIGHT_SSN);
}
/**
* Valida CNPJ
*
* @param tfn
* @return
*/
public static boolean isValidTfn(String tfn) {
if (tfn == null || !tfn.matches("\\d{14}") || tfn.matches(tfn.charAt(0) + "{14}")) return false;
return checkEquals(tfn, 12, WEIGHT_TFN);
}
}
@arrudawillian

This comment has been minimized.

Copy link

arrudawillian commented Dec 6, 2016

Excelente código e bem compacto, ajudou muito, valeu!

@Paulimjr

This comment has been minimized.

Copy link

Paulimjr commented Apr 16, 2017

Top, me ajudou bastante!

@Djavani

This comment has been minimized.

Copy link

Djavani commented Jan 8, 2018

Muito bom !

@lucasnpires

This comment has been minimized.

Copy link

lucasnpires commented Feb 26, 2018

Muito obrigado amigo!
Parabéns

@acsrede

This comment has been minimized.

Copy link

acsrede commented Mar 18, 2018

Muito obrigado Adriano, show seu código, muito util. Abraços!

@tarssito

This comment has been minimized.

Copy link

tarssito commented Mar 22, 2018

Muito Bom. Parabéns!

@Josegonzaga

This comment has been minimized.

Copy link

Josegonzaga commented Mar 23, 2018

Ótimo código Adriano, estou usando em um curso. Muito obrigado.

@jacksonezidio

This comment has been minimized.

Copy link

jacksonezidio commented Mar 26, 2018

Boa parabéns!

@Jsaiao

This comment has been minimized.

Copy link

Jsaiao commented Mar 29, 2018

Obrigado por compartilhar amigo, me ajudou muito em meu projeto.

@john61

This comment has been minimized.

Copy link

john61 commented May 20, 2018

Obrigado Adriano! Muito útil!

@jjcdutra2015

This comment has been minimized.

Copy link

jjcdutra2015 commented May 29, 2018

Parabéns!!! Muito bom.

@evertonalex

This comment has been minimized.

Copy link

evertonalex commented May 31, 2018

Muito bom Adriano, PARABÉNS!

@michelzarpe

This comment has been minimized.

Copy link

michelzarpe commented Jun 25, 2018

Grato, ajudou muito!

@projectstein

This comment has been minimized.

Copy link

projectstein commented Aug 1, 2018

Agradeço muito!

@HalyneDangelo

This comment has been minimized.

Copy link

HalyneDangelo commented Aug 9, 2018

Obrigada.

@rafamendes93

This comment has been minimized.

Copy link

rafamendes93 commented Aug 26, 2018

Obrigado!

@Koynonia

This comment has been minimized.

Copy link

Koynonia commented Aug 29, 2018

Ótimo código - compacto e eficiente! Muito obrigado!

@BrunoTardio

This comment has been minimized.

Copy link

BrunoTardio commented Sep 15, 2018

Agradeço!

@Gu1lh3rm

This comment has been minimized.

Copy link

Gu1lh3rm commented Sep 20, 2018

Muito bom obrigado!

@MaiconCanedo

This comment has been minimized.

Copy link

MaiconCanedo commented Sep 21, 2018

Grato!

@pedroteixeirazup

This comment has been minimized.

Copy link

pedroteixeirazup commented Sep 24, 2018

Ajudou bastante!

@thiagobarso

This comment has been minimized.

Copy link

thiagobarso commented Oct 19, 2018

Obrigado! Deus lhe abençoe!

@MarceloCarvalhoLopes

This comment has been minimized.

Copy link

MarceloCarvalhoLopes commented Dec 13, 2018

Obrigado por compartilhar conosco !

@danielrgn

This comment has been minimized.

Copy link

danielrgn commented Jan 14, 2019

Top! Obrigado!

@gutopersan

This comment has been minimized.

Copy link

gutopersan commented Jan 20, 2019

Excelente Adriano, parabéns !

@marcoscouto

This comment has been minimized.

Copy link

marcoscouto commented Jan 30, 2019

Vim pelo curso do Nélio Alves, obrigado por compartilhar seu conhecimento!!

@mukasc

This comment has been minimized.

Copy link

mukasc commented Feb 2, 2019

Valeu man!

@emmanoelpaim

This comment has been minimized.

Copy link

emmanoelpaim commented Feb 28, 2019

Obrigado pelo compartilhamento vim pelo curso do Nélio Alves

@Benjamim-Thiago

This comment has been minimized.

Copy link

Benjamim-Thiago commented Mar 13, 2019

Obrigado por compartilhar.

@flpoliveira

This comment has been minimized.

Copy link

flpoliveira commented Apr 1, 2019

Código excelente.

@rodrix

This comment has been minimized.

Copy link

rodrix commented Apr 22, 2019

Valeu lindo. :*

@guirald

This comment has been minimized.

Copy link

guirald commented Apr 27, 2019

Tks 4Sharing

@JoseCaarlos

This comment has been minimized.

Copy link

JoseCaarlos commented Jun 6, 2019

Muito Bom !!!

@renandare

This comment has been minimized.

Copy link

renandare commented Jun 7, 2019

TKS 4sharing!

@BZR4

This comment has been minimized.

Copy link

BZR4 commented Jun 18, 2019

Muitísismo obrigado por compartilhar! Ajudou muito!

@brunotoshiaki

This comment has been minimized.

Copy link

brunotoshiaki commented Jun 20, 2019

Obrigado por compartilhar. Ajudou muito.

@robersonupc

This comment has been minimized.

Copy link

robersonupc commented Jun 27, 2019

Obrigado André Luis por um código enxuto, ajudou e muito!!! Valeu!

@jaderdeandrade

This comment has been minimized.

Copy link

jaderdeandrade commented Jul 11, 2019

Obrigado Adriano! Ajudou muito.

@alanfalcoski

This comment has been minimized.

Copy link

alanfalcoski commented Sep 4, 2019

Obrigado!

@RodrigoVazOliveira

This comment has been minimized.

Copy link

RodrigoVazOliveira commented Sep 24, 2019

Muito bom, Obrigado!

@DouglasEleuterio

This comment has been minimized.

Copy link

DouglasEleuterio commented Sep 30, 2019

Muito bom, obrigado.
Ajudou muitos os alunos de um curso.
CursoMC

@adrianoluis

This comment has been minimized.

Copy link
Owner Author

adrianoluis commented Sep 30, 2019

Muito bom, obrigado.
Ajudou muitos os alunos de um curso.
CursoMC

Qual curso e de onde?!

@lucas-50p

This comment has been minimized.

Copy link

lucas-50p commented Oct 1, 2019

Muito bom, obrigado.
curso do Nélio Alves

@sostenesantunes

This comment has been minimized.

Copy link

sostenesantunes commented Oct 25, 2019

Muito bom, obrigado.
Ótimo conteúdo!

@brunotfnascimento

This comment has been minimized.

Copy link

brunotfnascimento commented Oct 26, 2019

Muito boa!
Obrigado por compartilhar!

@danilolosi

This comment has been minimized.

Copy link

danilolosi commented Oct 31, 2019

Obrigado!

@drikones

This comment has been minimized.

Copy link

drikones commented Nov 10, 2019

obrigado por compartilhar, Adriano!
do curso da Udemy do Nelio Alves....

@GeorgePaiva

This comment has been minimized.

Copy link

GeorgePaiva commented Dec 3, 2019

Obrigado por compartilhar!

@wcocink

This comment has been minimized.

Copy link

wcocink commented Dec 18, 2019

Obrigado por compartilhar o código

@Marcelolynx

This comment has been minimized.

Copy link

Marcelolynx commented Jan 10, 2020

thanks for sharing man! May the Force be with you!!!

@heliohdd

This comment has been minimized.

Copy link

heliohdd commented Jan 13, 2020

Excelente Adriano.
Obrigado!

@celiocausjunior

This comment has been minimized.

Copy link

celiocausjunior commented Feb 23, 2020

Excelente. Obrigado!

@SamuelTelesSilva

This comment has been minimized.

Copy link

SamuelTelesSilva commented Mar 28, 2020

thanks

@ednilton

This comment has been minimized.

Copy link

ednilton commented Mar 30, 2020

Thx Bro!

@marcbitenl

This comment has been minimized.

Copy link

marcbitenl commented Apr 3, 2020

Muito bom, ajudou muito
obrigado Adriano!

@SamillyNunes

This comment has been minimized.

Copy link

SamillyNunes commented Apr 11, 2020

Muito obrigada!!!

@brunoAbbade

This comment has been minimized.

Copy link

brunoAbbade commented Apr 20, 2020

Top D+. Obrigado.

@newcaiosantos

This comment has been minimized.

Copy link

newcaiosantos commented Apr 21, 2020

Simplesmente fantástico! Muito obrigado!

@shiromabruno

This comment has been minimized.

Copy link

shiromabruno commented Apr 27, 2020

Muito obrigado.

@neogedom

This comment has been minimized.

Copy link

neogedom commented May 4, 2020

Obrigado pelo código, Adriano!

@Stiverson

This comment has been minimized.

Copy link

Stiverson commented May 5, 2020

Obrigado pelo código amigo ^^.

@josuefonseca

This comment has been minimized.

Copy link

josuefonseca commented May 8, 2020

Excelente trabalho!

@escotiel

This comment has been minimized.

Copy link

escotiel commented Jul 9, 2020

Bom dia Adriano!
Obrigado por disponibilizar este código.
Me ajudou muito!

Abraço!

@macieleds

This comment has been minimized.

Copy link

macieleds commented Jul 20, 2020

Olá, Adriano!
Muito obrigado por disponibilizar o código. Será muito útil pra mim.
Muito sucesso pra ti.

Abraço!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.