Last active
June 2, 2021 01:53
-
-
Save Kauavitorio/fc90ff32993ebd31663d1bca43962045 to your computer and use it in GitHub Desktop.
Method for validating CPF in java
This file contains hidden or 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
private static final int[] WeightCPF = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2}; | |
public static boolean isValidCPF(String cpf) { | |
cpf = cpf.trim().replace(".", "").replace("-", ""); | |
if (cpf.length() != 11) return false; | |
for (int j = 0; j < 10; j++) | |
if (padLeft(Integer.toString(j), Character.forDigit(j, 10)).equals(cpf)) | |
return false; | |
int Digit01 = calculateDigit(cpf.substring(0,9)); | |
int Digit02 = calculateDigit(cpf.substring(0,9) + Digit01); | |
return cpf.equals(cpf.substring(0,9) + Digit01 + Digit02); | |
} | |
private static int calculateDigit(String str) { | |
int sum = 0; | |
for (int index =str.length()-1, digit; index >= 0; index-- ) { | |
digit = Integer.parseInt(str.substring(index,index+1)); | |
sum += digit* WeightCPF[ WeightCPF.length-str.length()+index]; | |
} | |
sum = 11 - sum % 11; | |
return sum > 9 ? 0 : sum; | |
} | |
private static String padLeft(String text, char character) { | |
return String.format("%11s", text).replace(' ', character); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To call this method: