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 arrudawillian commented Dec 6, 2016

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

@Paulimjr

This comment has been minimized.

Copy link

@Paulimjr Paulimjr commented Apr 16, 2017

Top, me ajudou bastante!

@Djavani

This comment has been minimized.

Copy link

@Djavani Djavani commented Jan 8, 2018

Muito bom !

@lucasnpires

This comment has been minimized.

Copy link

@lucasnpires lucasnpires commented Feb 26, 2018

Muito obrigado amigo!
Parabéns

@acsrede

This comment has been minimized.

Copy link

@acsrede 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 tarssito commented Mar 22, 2018

Muito Bom. Parabéns!

@Josegonzaga

This comment has been minimized.

Copy link

@Josegonzaga 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 jacksonezidio commented Mar 26, 2018

Boa parabéns!

@Jsaiao

This comment has been minimized.

Copy link

@Jsaiao Jsaiao commented Mar 29, 2018

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

@john61

This comment has been minimized.

Copy link

@john61 john61 commented May 20, 2018

Obrigado Adriano! Muito útil!

@jjcdutra2015

This comment has been minimized.

Copy link

@jjcdutra2015 jjcdutra2015 commented May 29, 2018

Parabéns!!! Muito bom.

@evertonalex

This comment has been minimized.

Copy link

@evertonalex evertonalex commented May 31, 2018

Muito bom Adriano, PARABÉNS!

@michelzarpe

This comment has been minimized.

Copy link

@michelzarpe michelzarpe commented Jun 25, 2018

Grato, ajudou muito!

@projectstein

This comment has been minimized.

Copy link

@projectstein projectstein commented Aug 1, 2018

Agradeço muito!

@HalyneDangelo

This comment has been minimized.

Copy link

@HalyneDangelo HalyneDangelo commented Aug 9, 2018

Obrigada.

@rafamendes93

This comment has been minimized.

Copy link

@rafamendes93 rafamendes93 commented Aug 26, 2018

Obrigado!

@Koynonia

This comment has been minimized.

Copy link

@Koynonia Koynonia commented Aug 29, 2018

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

@BrunoTardio

This comment has been minimized.

Copy link

@BrunoTardio BrunoTardio commented Sep 15, 2018

Agradeço!

@Gu1lh3rm

This comment has been minimized.

Copy link

@Gu1lh3rm Gu1lh3rm commented Sep 20, 2018

Muito bom obrigado!

@MaiconCanedo

This comment has been minimized.

Copy link

@MaiconCanedo MaiconCanedo commented Sep 21, 2018

Grato!

@pedroteixeirazup

This comment has been minimized.

Copy link

@pedroteixeirazup pedroteixeirazup commented Sep 24, 2018

Ajudou bastante!

@thiagobarso

This comment has been minimized.

Copy link

@thiagobarso thiagobarso commented Oct 19, 2018

Obrigado! Deus lhe abençoe!

@MarceloCarvalhoLopes

This comment has been minimized.

Copy link

@MarceloCarvalhoLopes MarceloCarvalhoLopes commented Dec 13, 2018

Obrigado por compartilhar conosco !

@danielrgn

This comment has been minimized.

Copy link

@danielrgn danielrgn commented Jan 14, 2019

Top! Obrigado!

@gutopersan

This comment has been minimized.

Copy link

@gutopersan gutopersan commented Jan 20, 2019

Excelente Adriano, parabéns !

@marcoscouto

This comment has been minimized.

Copy link

@marcoscouto 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 mukasc commented Feb 2, 2019

Valeu man!

@emmanoelpaim

This comment has been minimized.

Copy link

@emmanoelpaim 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 Benjamim-Thiago commented Mar 13, 2019

Obrigado por compartilhar.

@flpoliveira

This comment has been minimized.

Copy link

@flpoliveira flpoliveira commented Apr 1, 2019

Código excelente.

@rodrix

This comment has been minimized.

Copy link

@rodrix rodrix commented Apr 22, 2019

Valeu lindo. :*

@guirald

This comment has been minimized.

Copy link

@guirald guirald commented Apr 27, 2019

Tks 4Sharing

@JoseCaarlos

This comment has been minimized.

Copy link

@JoseCaarlos JoseCaarlos commented Jun 6, 2019

Muito Bom !!!

@renandare

This comment has been minimized.

Copy link

@renandare renandare commented Jun 7, 2019

TKS 4sharing!

@BZR4

This comment has been minimized.

Copy link

@BZR4 BZR4 commented Jun 18, 2019

Muitísismo obrigado por compartilhar! Ajudou muito!

@brunotoshiaki

This comment has been minimized.

Copy link

@brunotoshiaki brunotoshiaki commented Jun 20, 2019

Obrigado por compartilhar. Ajudou muito.

@robersonupc

This comment has been minimized.

Copy link

@robersonupc 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 jaderdeandrade commented Jul 11, 2019

Obrigado Adriano! Ajudou muito.

@alanfalcoski

This comment has been minimized.

Copy link

@alanfalcoski alanfalcoski commented Sep 4, 2019

Obrigado!

@RodrigoVazOliveira

This comment has been minimized.

Copy link

@RodrigoVazOliveira RodrigoVazOliveira commented Sep 24, 2019

Muito bom, Obrigado!

@DouglasEleuterio

This comment has been minimized.

Copy link

@DouglasEleuterio 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 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 lucas-50p commented Oct 1, 2019

Muito bom, obrigado.
curso do Nélio Alves

@sostenesantunes

This comment has been minimized.

Copy link

@sostenesantunes sostenesantunes commented Oct 25, 2019

Muito bom, obrigado.
Ótimo conteúdo!

@brunotfnascimento

This comment has been minimized.

Copy link

@brunotfnascimento brunotfnascimento commented Oct 26, 2019

Muito boa!
Obrigado por compartilhar!

@danilolosi

This comment has been minimized.

Copy link

@danilolosi danilolosi commented Oct 31, 2019

Obrigado!

@drikones

This comment has been minimized.

Copy link

@drikones 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 GeorgePaiva commented Dec 3, 2019

Obrigado por compartilhar!

@wcocink

This comment has been minimized.

Copy link

@wcocink wcocink commented Dec 18, 2019

Obrigado por compartilhar o código

@Marcelolynx

This comment has been minimized.

Copy link

@Marcelolynx Marcelolynx commented Jan 10, 2020

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

@heliohdd

This comment has been minimized.

Copy link

@heliohdd heliohdd commented Jan 13, 2020

Excelente Adriano.
Obrigado!

@celiocausjunior

This comment has been minimized.

Copy link

@celiocausjunior celiocausjunior commented Feb 23, 2020

Excelente. Obrigado!

@SamuelTelesSilva

This comment has been minimized.

Copy link

@SamuelTelesSilva SamuelTelesSilva commented Mar 28, 2020

thanks

@ednilton

This comment has been minimized.

Copy link

@ednilton ednilton commented Mar 30, 2020

Thx Bro!

@marcbitenl

This comment has been minimized.

Copy link

@marcbitenl marcbitenl commented Apr 3, 2020

Muito bom, ajudou muito
obrigado Adriano!

@SamillyNunes

This comment has been minimized.

Copy link

@SamillyNunes SamillyNunes commented Apr 11, 2020

Muito obrigada!!!

@brunoAbbade

This comment has been minimized.

Copy link

@brunoAbbade brunoAbbade commented Apr 20, 2020

Top D+. Obrigado.

@newcaiosantos

This comment has been minimized.

Copy link

@newcaiosantos newcaiosantos commented Apr 21, 2020

Simplesmente fantástico! Muito obrigado!

@shiromabruno

This comment has been minimized.

Copy link

@shiromabruno shiromabruno commented Apr 27, 2020

Muito obrigado.

@neogedom

This comment has been minimized.

Copy link

@neogedom neogedom commented May 4, 2020

Obrigado pelo código, Adriano!

@Stiverson

This comment has been minimized.

Copy link

@Stiverson Stiverson commented May 5, 2020

Obrigado pelo código amigo ^^.

@josuefonseca

This comment has been minimized.

Copy link

@josuefonseca josuefonseca commented May 8, 2020

Excelente trabalho!

@escotiel

This comment has been minimized.

Copy link

@escotiel 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 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!

@santosediego

This comment has been minimized.

Copy link

@santosediego santosediego commented Aug 13, 2020

Muito obrigado Adriano!

@Romaomoura

This comment has been minimized.

Copy link

@Romaomoura Romaomoura commented Aug 17, 2020

Valeu, ajudou demais!

@Pipiluco

This comment has been minimized.

Copy link

@Pipiluco Pipiluco commented Aug 19, 2020

Obrigado

@nasseroth

This comment has been minimized.

Copy link

@nasseroth nasseroth commented Aug 21, 2020

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

@qmclouca

This comment has been minimized.

Copy link

@qmclouca qmclouca commented Sep 6, 2020

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

@rasec23rj

This comment has been minimized.

Copy link

@rasec23rj rasec23rj commented Sep 18, 2020

Muito bom Obg!

@wep1980

This comment has been minimized.

Copy link

@wep1980 wep1980 commented Oct 5, 2020

Nota 10!

@lucassverissimo

This comment has been minimized.

Copy link

@lucassverissimo lucassverissimo commented Oct 8, 2020

Valeu, cara. Ajudou muito!!

@LeonilSouza

This comment has been minimized.

Copy link

@LeonilSouza LeonilSouza commented Oct 9, 2020

Maravilha, obrigado. Deus abençoe.

@vanderson22

This comment has been minimized.

Copy link

@vanderson22 vanderson22 commented Oct 13, 2020

Muito Obrigado!

@lucasfrancisco

This comment has been minimized.

Copy link

@lucasfrancisco lucasfrancisco commented Oct 23, 2020

Obrigado por compartilhar, grande ajuda!

@MarinhoFeliphe

This comment has been minimized.

Copy link

@MarinhoFeliphe MarinhoFeliphe commented Oct 27, 2020

Valeu mesmo!

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.