-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
156 lines (128 loc) · 3.44 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Faz a validação do CPF
* @description
* 10/05/2022 vlima
*/
function validarCPF(cpf) {
//Remove mascara
cpf = cpf.replace(/[^\d]+/g, "");
if (
cpf.length !== 11 ||
cpf === "00000000000" ||
cpf === "11111111111" ||
cpf === "22222222222" ||
cpf === "33333333333" ||
cpf === "44444444444" ||
cpf === "55555555555" ||
cpf === "66666666666" ||
cpf === "77777777777" ||
cpf === "88888888888" ||
cpf === "99999999999"
) {
return false;
}
var soma = 0;
//Soma os 9 primeiros Digitos multiplicados de 10 a 2
for (var i = 0; i < 9; i++) {
soma += cpf.charAt(i) * (10 - i);
}
// Validação do Primeiro Digito
var resultado = soma % 11 < 2 ? 0 : 11 - (soma % 11);
if (resultado != cpf.charAt(9)) {
return false;
}
soma = 0;
//Soma os 10 primeiros Digitos multiplicados de 11 a 2
for (var k = 0; k < 10; k++) {
soma += cpf.charAt(k) * (11 - k);
}
// Validação do Segundo Digito
resultado = soma % 11 < 2 ? 0 : 11 - (soma % 11);
if (resultado != cpf.charAt(10)) {
return false;
}
return true;
}
/**
* Mostra em Tela , se o CPF é valido ou Inválido
* @description
* 10/05/2022 vlima
*/
function validacao() {
//Limpar Mensagem
document.getElementById("success").style.display = "none";
document.getElementById("error").style.display = "none";
//Recebe Dados digitados pelo usuario
let cpf = document.getElementById("cpf_digitado").value;
let resultadovalidacao = validarCPF(cpf);
// Mostra Resultado em Tela
if (resultadovalidacao) {
document.getElementById("success").style.display = "block";
document.getElementById("origem").innerHTML = this.validaOrigemCPF(cpf);
} else {
document.getElementById("error").style.display = "block";
}
}
/**
* Validação da cidade de Origem do CPF, através do digito
* @description
* 11/05/2022 vlima
*/
function validaOrigemCPF(cpf) {
//Valida Origem do CPF através do 8 digito do CPF
switch (cpf.charAt(10)) {
case "1":
return " Distrito Federal, Goiás, Mato Grosso do Sul ou Tocantins";
case "2":
return " Pará, Amazonas, Acre, Amapá, Rondônia ou Roraima";
case "3":
return " Ceará, Maranhão ou Piau";
case "4":
return " Pernambuco, Rio Grande do Norte, Paraíba ou Alagoas";
case "5":
return " Bahia ou Sergipe";
case "6":
return " Minas Gerais";
case "7":
return " Rio de Janeiro ou Espírito Santo";
case "8":
return " São Paulo";
case "9":
return " Paraná ou Santa Catarina";
case "0":
return " Rio Grande do Sul";
}
}
/**
* Adiciona Mascara de CPF
* @description
* 06/06/2022 vlima
*/
function mascaraCPF() {
let cpf = document.getElementById("cpf_digitado");
//Adiciona Ponto nos lugares corretos
if (cpf.value.length == 3 || cpf.value.length == 7) {
cpf.value += ".";
}
//Adiciona Traço no lugar correto
if (cpf.value.length == 11) {
cpf.value += "-";
}
cursorInput();
}
/**
* Adiciona Tratamento na Posição do Cursor
* @description
* 06/06/2022 vlima
*/
function cursorInput() {
let cpf = document.getElementById("cpf_digitado");
let cursor = cpf.selectionStart;
let tecla = window.event ? event.keyCode : event.which;
//Tratamento na Posição do Cursor
if (tecla != 37 && (cursor == 3 || cursor == 7 || cursor == 11)) {
cpf.setSelectionRange(cursor + 1, cursor + 1);
} else {
cpf.setSelectionRange(cursor, cursor);
}
}