-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (50 loc) · 1.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Based on article: https://www.geradorcpf.com/javascript-validar-cpf.htm
*/
export const validateCPF = cpf => {
var rev;
cpf = cpf.replace(/[^\d]+/g, '');
const knownCPFInvalid = [
"00000000000",
"11111111111",
"22222222222",
"33333333333",
"44444444444",
"55555555555",
"66666666666",
"77777777777",
"88888888888",
"99999999999",
];
const hasAnIncorrectLenght = cpf.length != 11;
const thereIsInknownCPFInvalid = knownCPFInvalid.indexOf(cpf) > -1;
const isEmpty = '';
const getRevFromDigitPosition = (digitPosition) => {
var add = 0;
for (var i = 0; i < digitPosition; i++) {
add += parseInt(cpf.charAt(i)) * ((digitPosition + 1) - i);
}
rev = 11 - (add % 11);
if (rev == 10 || rev == 11) {
rev = 0;
}
return rev;
}
const checkIfRevIsValidFromDigitPosition = (digitPosition) => {
var rev = getRevFromDigitPosition(digitPosition);
return rev == parseInt(cpf.charAt(digitPosition));
}
// check if cpf is valid
if (isEmpty || thereIsInknownCPFInvalid || hasAnIncorrectLenght) {
return false;
}
// validate first digit
if (!checkIfRevIsValidFromDigitPosition(9)) {
return false;
}
// validate second digit
if (!checkIfRevIsValidFromDigitPosition(10)) {
return false;
}
return true;
}