-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
64 lines (45 loc) · 1.5 KB
/
script.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
/* mode é operador %
operador ternario */
console.log('Javascript carregado');
const cpf = '()';
const regex = new RegExp('[0-9]{3}.[0-9]{3}.[0-9]{3}.[0-9]{2}');
function validaCPF(cpf) {
if (cpf.length != 11) {
return false;
} else {
var numeros = cpf.substring(0, 9);
var digitos = cpf.substring(9);
var soma = 0;
for (var i = 10; i > 1; i--) {
soma += numeros.charAt(10 - i) * i;
}
console.log(soma);
var resultado = soma % 11 < 2 ? 0 : 11 - (soma % 11);
//valida primeiro digito//
if (resultado != digitos.charAt(0)) {
return false;
}
soma = 0;
numeros = cpf.substring(0, 10);
for (var k = 11; k > 1; k--) {
soma += numeros.charAt(11 - k) * k;
}
resultado = soma % 11 < 2 ? 0 : 11 - (soma % 11);
if (resultado != digitos.charAt(1)){
return false;
}
return true;
}
}
function validacao() {
console.log('Iniciando validação CPF');
document.getElementById('success').style.display = 'none';
document.getElementById('error').style.display = 'none';
var cpf = document.getElementById('cpf_digitado').value;
var resultadoValidacao = validaCPF(cpf);
if (resultadoValidacao) {
document.getElementById('success').style.display = 'block';
} else {
document.getElementById('error').style.display = 'block';
}
}