|
@@ -1,4 +1,26 @@
|
|
|
-const validatecpf = (cpf) => {
|
|
|
+'use strict';
|
|
|
+
|
|
|
+window.onload = () => {
|
|
|
+ if ('serviceWorker' in navigator) {
|
|
|
+ navigator.serviceWorker.register('./sw.js');
|
|
|
+ console.info('serviceWorker loaded!');
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+var formulario = {};
|
|
|
+(function(formulario){
|
|
|
+ formulario.cpf = document.getElementById('cpf');
|
|
|
+ formulario.bairro = document.getElementById('bairro');
|
|
|
+ formulario.endereco = document.getElementById('endereco')
|
|
|
+ formulario.cidade = document.getElementById('cidade');
|
|
|
+ formulario.estado = document.getElementById('estado');
|
|
|
+ formulario.celular = document.getElementById('celular');
|
|
|
+ formulario.required = document.querySelectorAll(".required");
|
|
|
+ formulario.invalidElement = document.querySelector("span");
|
|
|
+ console.log(formulario);
|
|
|
+})(formulario);
|
|
|
+
|
|
|
+function validatecpf(cpf) {
|
|
|
if (cpf.length != 11) return false;
|
|
|
|
|
|
let numbers = cpf.substring(0, 9)
|
|
@@ -25,34 +47,31 @@ const validatecpf = (cpf) => {
|
|
|
}
|
|
|
|
|
|
function validate() {
|
|
|
-
|
|
|
-
|
|
|
- const cpf = document.getElementById("cpf").value.replace(/[^a-z0-9 ]/g, "")
|
|
|
- const validInputs = document.querySelectorAll(".required")
|
|
|
- let inputIsValid = validatecpf(cpf)
|
|
|
- const cpfIsValid = validatecpf(cpf)
|
|
|
-
|
|
|
- for (i = 0; i < validInputs.length; i++) {
|
|
|
- if (validInputs[i].value === "") validInputs[i].style["border"] = "1px solid red"
|
|
|
- }
|
|
|
- for (i = 0; i < validInputs.length; i++) {
|
|
|
- if (validInputs[i].value === "") inputIsValid = false;
|
|
|
- else {
|
|
|
+ const cpf = formulario.cpf.value.replace(/[^a-z0-9 ]/g, "")
|
|
|
+ const requiredInputs = formulario.required;
|
|
|
+ let inputIsValid = false;
|
|
|
+ const cpfIsValid = validatecpf(cpf);
|
|
|
+
|
|
|
+ for (let i = 0; i < requiredInputs.length; i++) {
|
|
|
+ if (requiredInputs[i].value === "") {
|
|
|
+ requiredInputs[i].style["border"] = "1px solid red"
|
|
|
+ inputIsValid = false;
|
|
|
+ } else {
|
|
|
inputIsValid = true
|
|
|
- validInputs[1].style["border"] = "none"
|
|
|
- document.querySelector("span").style["display"] = "none"
|
|
|
- validInputs[i].style["border"] = "none"
|
|
|
+ requiredInputs[1].style["border"] = "none"
|
|
|
+ formulario.invalidElement.style["display"] = "none"
|
|
|
+ requiredInputs[i].style["border"] = "none"
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (!inputIsValid || !cpfIsValid) {
|
|
|
window.alert("Opss! Aconteceu um erro no seu cadastro, verifique se você preencheu todos espaços necessários")
|
|
|
cpfIsValid ? (
|
|
|
- validInputs[1].style["border"] = "none",
|
|
|
- document.querySelector("span").style["display"] = "none"
|
|
|
+ requiredInputs[1].style["border"] = "none",
|
|
|
+ formulario.invalidElement.style["display"] = "none"
|
|
|
) : (
|
|
|
- validInputs[1].style["border"] = "1px solid red",
|
|
|
- document.querySelector("span").style["display"] = "inline"
|
|
|
+ requiredInputs[1].style["border"] = "1px solid red",
|
|
|
+ formulario.invalidElement.style["display"] = "inline"
|
|
|
);
|
|
|
return false
|
|
|
}
|
|
@@ -60,70 +79,71 @@ function validate() {
|
|
|
return true
|
|
|
}
|
|
|
function clearCepInputs() {
|
|
|
- document.getElementById('endereco').value = ("");
|
|
|
- document.getElementById('bairro').value = ("");
|
|
|
- document.getElementById('cidade').value = ("");
|
|
|
- document.getElementById('estado').value = ("");
|
|
|
+ formulario.endereco.value = ("");
|
|
|
+ formulario.bairro.value = ("");
|
|
|
+ formulario.cidade.value = ("");
|
|
|
+ dformulario.estado.value = ("");
|
|
|
}
|
|
|
|
|
|
function retrievingCepInputs(cepInputs) {
|
|
|
if ('erro' in cepInputs) {
|
|
|
clearCepInputs();
|
|
|
alert("CEP não encontrado.");
|
|
|
- }else {
|
|
|
- document.getElementById('endereco').value = (cepInputs.logradouro);
|
|
|
- document.getElementById('bairro').value = (cepInputs.bairro);
|
|
|
- document.getElementById('cidade').value = (cepInputs.localidade);
|
|
|
- document.getElementById('estado').value = (cepInputs.uf);
|
|
|
+ return;
|
|
|
}
|
|
|
+ formulario.endereco.value = (cepInputs.logradouro);
|
|
|
+ formulario.bairro.value = (cepInputs.bairro);
|
|
|
+ formulario.cidade.value = (cepInputs.localidade);
|
|
|
+ formulario.estado.value = (cepInputs.uf);
|
|
|
}
|
|
|
|
|
|
function searchCep(valor) {
|
|
|
|
|
|
const cep = valor.replace(/\D/g, '');
|
|
|
|
|
|
- if (cep === "") clearCepInputs();
|
|
|
- else {
|
|
|
- const validateCep = /^[0-9]{8}$/;
|
|
|
- if (validateCep.test(cep)) {
|
|
|
- let script = document.createElement('script');
|
|
|
- script.src = 'https://viacep.com.br/ws/' + cep + '/json/?callback=retrievingCepInputs';
|
|
|
- document.body.appendChild(script);
|
|
|
-
|
|
|
- } else {
|
|
|
- clearCepInputs();
|
|
|
- alert("Formato de CEP inválido.");
|
|
|
- }
|
|
|
+ if (cep === "") {
|
|
|
+ clearCepInputs();
|
|
|
+ return;
|
|
|
}
|
|
|
+
|
|
|
+ const validateCep = /^[0-9]{8}$/;
|
|
|
+ if (! validateCep.test(cep)) {
|
|
|
+ clearCepInputs();
|
|
|
+ alert("Formato de CEP inválido.");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const script = document.createElement('script');
|
|
|
+ script.src = 'https://viacep.com.br/ws/' + cep + '/json/?callback=retrievingCepInputs';
|
|
|
+ document.body.appendChild(script);
|
|
|
+
|
|
|
};
|
|
|
|
|
|
function cpfMask() {
|
|
|
- let i = document.getElementById("cpf").value.length;
|
|
|
- if (i === 3 || i === 7)
|
|
|
- document.getElementById("cpf").value = document.getElementById("cpf").value + ".";
|
|
|
- else if (i === 11)
|
|
|
- document.getElementById("cpf").value = document.getElementById("cpf").value + "-";
|
|
|
- else if (i===14) i = 0
|
|
|
- }
|
|
|
+ const length = formulario.cpf.value.length;
|
|
|
+ if (length === 3 || length === 7)
|
|
|
+ formulario.cpf.value = formulario.cpf.value + ".";
|
|
|
+ else if (length === 11)
|
|
|
+ formulario.cpf.value = formulario.cpf.value + "-";
|
|
|
+ else if (length===14) length = 0
|
|
|
+}
|
|
|
|
|
|
function phoneMask() {
|
|
|
- let i = document.getElementById('celular').value.length;
|
|
|
- switch (i) {
|
|
|
+ const length = formulario.celular.value.length;
|
|
|
+ switch (length) {
|
|
|
case 0:
|
|
|
- document.getElementById('celular').value = '('
|
|
|
+ formulario.celular.value = '('
|
|
|
break
|
|
|
case 3:
|
|
|
- document.getElementById('celular').value += ')'
|
|
|
+ formulario.celular.value += ')'
|
|
|
break
|
|
|
case 5:
|
|
|
- document.getElementById('celular').value += ' ';
|
|
|
+ formulario.celular.value += ' ';
|
|
|
break
|
|
|
case 10:
|
|
|
- document.getElementById('celular').value += '-'
|
|
|
+ formulario.celular.value += '-'
|
|
|
break
|
|
|
case 15:
|
|
|
i = 0
|
|
|
- break
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
+}
|