-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
67 lines (56 loc) · 1.7 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
59
60
61
62
63
64
65
66
67
/*
Based on https://www.geradorcnpj.com/javascript-validar-cnpj.htm
*/
export const validateCNPJ = (cnpj) => {
var size, numbers, digits, sum, pos;
cnpj = String(cnpj).replace(/[^\d]+/g, '');
const knownInvalidValues = [
"00000000000",
"11111111111",
"22222222222",
"33333333333",
"44444444444",
"55555555555",
"66666666666",
"77777777777",
"88888888888",
"99999999999",
];
const hasAnIncorrectLenght = cnpj.length != 14
const isThereInknownInvalidValues = knownInvalidValues.indexOf(cnpj) > -1;
const isEmpty = cnpj == '';
if (hasAnIncorrectLenght || isThereInknownInvalidValues || isEmpty) {
return false;
}
// Valida DVs
size = cnpj.length - 2
numbers = cnpj.substring(0, size);
digits = cnpj.substring(size);
sum = 0;
pos = size - 7;
for (var i = size; i >= 1; i--) {
sum += numbers.charAt(size - i) * pos--;
if (pos < 2) {
pos = 9;
}
}
result = sum % 11 < 2 ? 0 : 11 - sum % 11;
if (result != digits.charAt(0)) {
return false;
}
size = size + 1;
numbers = cnpj.substring(0, size);
sum = 0;
pos = size - 7;
for (var i = size; i >= 1; i--) {
sum += numbers.charAt(size - i) * pos--;
if (pos < 2) {
pos = 9;
}
}
result = sum % 11 < 2 ? 0 : 11 - sum % 11;
if (result != digits.charAt(1)) {
return false;
}
return true;
}