-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
134 lines (112 loc) · 4.16 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
// ----funções gerais--- //
function togglePopup(input, label) {
// Mostrar popup de campo obrigatório
input.addEventListener("focus", () => {
label.classList.add("required-popup");
});
// Ocultar popup de campo obrigatório
input.addEventListener("blur", () => {
label.classList.remove("required-popup");
});
}
function estilizarInputCorreto(input, helper) {
helper.classList.remove("visible");
input.classList.remove("error");
input.classList.add("correct");
}
function estilizarInputIncorreto(input, helper) {
helper.classList.add("visible");
input.classList.add("error");
input.classList.remove("correct");
}
// ---------- VALIDAÇÃO USERNAME ---------- //
let usernameInput = document.getElementById("username");
let usernameLabel = document.querySelector('label[for="username"]');
let usernameHelper = document.getElementById("username-helper");
togglePopup(usernameInput, usernameLabel);
// Validar valor do input
usernameInput.addEventListener("change", (e) => {
let valor = e.target.value;
if (valor.length < 3) {
// Adicionar estilos dinâmicos se o valor tiver menos de 3 caracteres
usernameHelper.innerText = "Seu username precisa ter 3 ou mais caracteres";
estilizarInputIncorreto(usernameInput, usernameHelper);
inputsCorretos.username = false;
} else {
// Adicionar estilos dinâmicos se o valor estiver correto
estilizarInputCorreto(usernameInput, usernameHelper);
inputsCorretos.username = true;
}
});
// ---- VALIDAÇÃO EMAIL ---- //
let emailInput = document.getElementById("email");
let emailLabel = document.querySelector('label[for="email"]');
let emailHelper = document.getElementById("email-helper");
togglePopup(emailInput, emailLabel);
// Validar valor do input
emailInput.addEventListener("change", (e) => {
let valor = e.target.value;
if (valor.includes("@") && valor.includes(".com")) {
// Adicionar estilos dinâmicos se o valor estiver correto
estilizarInputCorreto(emailInput, emailHelper);
inputsCorretos.email = true;
} else {
// Adicionar estilos dinâmicos se o valor tiver menos de 3 caracteres
emailHelper.innerText = "Precisa inserir um email válido";
estilizarInputIncorreto(emailInput, emailHelper);
inputsCorretos.email = false;
}
});
// ---- VALIDAÇÃO SENHA ----
let senhaInput = document.getElementById("senha");
let senhaLabel = document.querySelector('label[for="senha"]');
let senhaHelper = document.getElementById("senha-helper");
togglePopup(senhaInput, senhaLabel);
senhaInput.addEventListener("blur", (e) => {
let valor = e.target.value;
if (valor == "") {
senhaHelper.innerText = "O campo senha não pode estar vazio";
estilizarInputIncorreto(senhaInput, senhaHelper);
inputsCorretos.senha = false;
} else {
estilizarInputCorreto(senhaInput, senhaHelper);
inputsCorretos.senha = true;
}
});
// ---- VALIDAÇÃO CONFIRMA SENHA ----
let confirmaSenhaInput = document.getElementById("confirma-senha");
let confirmaSenhaLabel = document.querySelector('label[for="confirma-senha"]');
let confirmaSenhaHelper = document.getElementById("confirma-senha-helper");
togglePopup(confirmaSenhaInput, confirmaSenhaLabel);
confirmaSenhaInput.addEventListener("blur", (e) => {
let valorConfirmaSenha = e.target.value;
if (valorConfirmaSenha == senhaInput.value) {
estilizarInputCorreto(confirmaSenhaInput, confirmaSenhaHelper);
inputsCorretos.confirmaSenha = true;
} else {
confirmaSenhaHelper.innerText = "As senhas precisam ser iguais";
estilizarInputIncorreto(confirmaSenhaInput, confirmaSenhaHelper);
inputsCorretos.confirmaSenha = false;
}
});
// ---- EVITAR ENVIO DO FORMULÁRIO ---- //
let btnSubmit = document.querySelector('button[type="submit"]');
let inputsCorretos = {
username: false,
email: false,
senha: false,
confirmaSenha: false,
};
btnSubmit.addEventListener("click", (e) => {
if (
inputsCorretos.username == false ||
inputsCorretos.email == false ||
inputsCorretos.senha == false ||
inputsCorretos.confirmaSenha == false
) {
e.preventDefault();
alert("Os campos obrigatórios precisam ser preenchidos corretamento");
} else {
alert("Formulário enviado com sucesso");
}
});