-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
56 lines (41 loc) · 1.43 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
console.log('Javascript carregado!');
function validaCPF(cpf) {
if(cpf.length != 11) {
return false;
} else {
var numeros = cpf.substring(0,9); // pega os 9 primeiro dígitos
var digitos = cpf.substring(9); // pega apenas a partir do 9 digito
var soma = 0;
for( var i = 10; i > 1; i--) {
soma += numeros.charAt(10 - i) * i;
}
var resultado = soma % 11 < 2 ? 0 : 11 - (soma % 11);
// Validação 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);
// Validação segundo digito
if(resultado != digitos.charAt(1)) {
return false;
}
return true;
}
}
function validacao() {
console.log('Iniciando a validação do 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';
}
}