Created
April 26, 2024 13:32
-
-
Save cmdbot/88e609268d9dca7884bbd429151309a5 to your computer and use it in GitHub Desktop.
CNPJ / CPF (Federal Tax ID for Brasil) Validator in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CNPJValidator { | |
public static boolean validateCNPJ(String cnpj) { | |
// Remove non-numeric characters | |
cnpj = cnpj.replaceAll("[^0-9]", ""); | |
// Check if CNPJ has 14 digits | |
if (cnpj.length() != 14) | |
return false; | |
// Check if all digits are equal | |
boolean allDigitsEqual = true; | |
for (int i = 1; i < cnpj.length(); i++) { | |
if (cnpj.charAt(i) != cnpj.charAt(0)) { | |
allDigitsEqual = false; | |
break; | |
} | |
} | |
if (allDigitsEqual) | |
return false; | |
// Calculate the first verification digit | |
int sum = 0; | |
int[] weight1 = { 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 }; | |
for (int i = 0; i < 12; i++) { | |
sum += (cnpj.charAt(i) - '0') * weight1[i]; | |
} | |
int digit1 = 11 - (sum % 11); | |
if (digit1 >= 10) | |
digit1 = 0; | |
// Verify the first verification digit | |
if (digit1 != cnpj.charAt(12) - '0') | |
return false; | |
// Calculate the second verification digit | |
int[] weight2 = { 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 }; | |
sum = 0; | |
for (int i = 0; i < 13; i++) { | |
sum += (cnpj.charAt(i) - '0') * weight2[i]; | |
} | |
int digit2 = 11 - (sum % 11); | |
if (digit2 >= 10) | |
digit2 = 0; | |
// Verify the second verification digit | |
if (digit2 != cnpj.charAt(13) - '0') | |
return false; | |
// Valid CNPJ | |
return true; | |
} | |
public static void main(String[] args) { | |
//Sample | |
String cnpj = "12.345.678/0001-90"; | |
if (validateCNPJ(cnpj)) { | |
System.out.println("Valid CNPJ"); | |
} else { | |
System.out.println("Invalid CNPJ"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CPFValidator { | |
public static boolean validateCPF(String cpf) { | |
// Remove non-numeric characters | |
cpf = cpf.replaceAll("[^0-9]", ""); | |
// Check if CPF has 11 digits | |
if (cpf.length() != 11) | |
return false; | |
// Check if all digits are equal | |
boolean allDigitsEqual = true; | |
for (int i = 1; i < cpf.length(); i++) { | |
if (cpf.charAt(i) != cpf.charAt(0)) { | |
allDigitsEqual = false; | |
break; | |
} | |
} | |
if (allDigitsEqual) | |
return false; | |
// Calculate the first verification digit | |
int sum = 0; | |
for (int i = 0; i < 9; i++) { | |
sum += (cpf.charAt(i) - '0') * (10 - i); | |
} | |
int digit1 = 11 - (sum % 11); | |
if (digit1 >= 10) | |
digit1 = 0; | |
// Verify the first verification digit | |
if (digit1 != cpf.charAt(9) - '0') | |
return false; | |
// Calculate the second verification digit | |
sum = 0; | |
for (int i = 0; i < 10; i++) { | |
sum += (cpf.charAt(i) - '0') * (11 - i); | |
} | |
int digit2 = 11 - (sum % 11); | |
if (digit2 >= 10) | |
digit2 = 0; | |
// Verify the second verification digit | |
if (digit2 != cpf.charAt(10) - '0') | |
return false; | |
// Valid CPF | |
return true; | |
} | |
public static void main(String[] args) { | |
//Sample | |
String cpf = "123.456.789-09"; | |
if (validateCPF(cpf)) { | |
System.out.println("Valid CPF"); | |
} else { | |
System.out.println("Invalid CPF"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment