-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
187 lines (148 loc) · 5.99 KB
/
index.html
File metadata and controls
187 lines (148 loc) · 5.99 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<!DOCTYPE html>
<html lang="pt">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>Consulta e validação de CNPJ</title>
<!-- JQuery -->
<script src="https://code.jquery.com/jquery-3.6.3.min.js"></script>
<!-- Tailwind css -->
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<main class="flex flex-col gap-y-6 max-w-5xl mx-auto pt-20 px-4">
<h1 class="text-xl font-bold text-indigo-600">
Consulta e validação de CNPJ
</h1>
<span class="text-xs text-slate-500">
Utilizando a API <a href="https://docs.minhareceita.org/" target="_blank" class="text-indigo-500 underline font-medium">minhareceita.org </a>
</span>
<div class="w-full">
<label class="text-slate-500 text-sm font-medium block mb-2">
Digite o CNPJ:
</label>
<input id="CNPJ" class="px-2.5 py-1.5 border border-gray-400 rounded-md text-sm transition duration-500 focus:outline-none focus:border-indigo-500 w-full max-w-2xl" type="text" />
<div id="feedback"></div>
</div>
<h6 class="text-gray-600 text-sm" id="resultadoBusca"></h6>
</main>
</body>
<script>
function IsCnpj(cnpj) {
if (!cnpj) {
return false;
}
// Retorna só os números
cnpj = String(cnpj);
cnpj = cnpj.replace(/[^0-9]/g, '');
/**
* CNPJ tem 14 digitos, verifica se não é 14.
* Verifica se foi informada uma sequência de digitos repetidos.
*/
if (cnpj.length != 14 || cnpj.match('/(\d)\1{13}/')) {
return false;
}
/**
* Calculo para validar o CNPJ
*/
let tamanho = cnpj.length - 2
let numeros = cnpj.substring(0, tamanho);
let digitos = cnpj.substring(tamanho);
let soma = 0;
let pos = tamanho - 7;
for (i = tamanho; i >= 1; i--) {
soma += numeros.charAt(tamanho - i) * pos--;
if (pos < 2) {
pos = 9;
}
}
resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
if (resultado != digitos.charAt(0)) {
return false;
}
tamanho = tamanho + 1;
numeros = cnpj.substring(0, tamanho);
soma = 0;
pos = tamanho - 7;
for (i = tamanho; i >= 1; i--) {
soma += numeros.charAt(tamanho - i) * pos--;
if (pos < 2)
pos = 9;
}
resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
if (resultado != digitos.charAt(1)) {
return false;
}
return true;
}
document.querySelector('#CNPJ').addEventListener('input', () => {
let cnpj = document.querySelector('#CNPJ');
let cnpjValue = cnpj.value;
let isValid = IsCnpj(cnpjValue);
let feedback = document.querySelector('#feedback');
let textResult = document.querySelector('#resultadoBusca');
isValid ?
(cnpj.classList.add('input-isValid'),
cnpj.classList.remove('input-isNotValid'),
feedback.classList.add('feedback-isValid'),
feedback.classList.remove('feedback-isNotValid'),
feedback.innerText = 'CNPJ válido!'
)
:
(cnpj.classList.add('input-isNotValid'),
cnpj.classList.remove('input-isValid'),
feedback.classList.add('feedback-isNotValid'),
feedback.classList.remove('feedback-isValid'),
cnpjValue.length == 0 ? feedback.innerText = 'Este campo é obrigatório.' : feedback.innerText = 'Digite um CNPJ válido.');
// GET
function getCnpjData() {
let baseUrlReceita = 'https://minhareceita.org/' + cnpjValue;
$.get(baseUrlReceita, function (data, status) {
console.log(data);
if (status == 'success') {
textResult.innerText = 'Razão social: ' + data.razao_social + '\nNome fantasia: ' + data.nome_fantasia + '\n\nVerifique o console.log para ver todas as informações.'
} else {
textResult.innerText = data.message
}
});
}
// Show text result
isValid ? getCnpjData() : textResult.innerText = '';
})
</script>
<style>
.input-isValid,
.input-isValid:focus {
border-color: #16a34a !important;
background-image: url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" fill="green" viewBox="0 0 16 16"%3E%3Cpath d="M10.97 4.97a.75.75 0 0 1 1.07 1.05l-3.99 4.99a.75.75 0 0 1-1.08.02L4.324 8.384a.75.75 0 1 1 1.06-1.06l2.094 2.093 3.473-4.425a.267.267 0 0 1 .02-.022z"/%3E%3C/svg%3E');
background-position-x: calc(100% - 3px);
background-position-y: 6px;
background-repeat: no-repeat;
background-size: 22px;
}
.input-isNotValid,
.input-isNotValid:focus {
border-color: #ff0000 !important;
background-image: url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" fill="red" viewBox="0 0 16 16"%3E%3Cpath d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/%3E%3Cpath d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z"/%3E%3C/svg%3E');
background-position-x: calc(100% - 6px);
background-position-y: 9px;
background-repeat: no-repeat;
background-size: 14px;
}
#feedback {
display: none;
font-size: 12px;
font-weight: 400;
margin-top: .5rem;
}
#feedback.feedback-isNotValid {
display: block;
color: #e90606;
}
#feedback.feedback-isValid {
display: block;
color: #16a34a;
}
</style>
</html>