-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidaciones.js
55 lines (50 loc) · 1.51 KB
/
validaciones.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
export function validarInputs() {
const formInputs = document.querySelectorAll(".contacto__campo-form");
formInputs.forEach((formInput) => {
formInput.addEventListener("blur", (formInput) => {
valida(formInput.target);
})
});
}
function valida(formInput) {
const tipoDeformInput = formInput.dataset.tipo;
if (formInput.validity.valid) {
formInput.parentElement.classList.remove("datos__erroneos");
formInput.parentElement.querySelector(".campo__error").innerHTML = "";
}else {
formInput.parentElement.classList.add("datos__erroneos");
formInput.parentElement.querySelector(".campo__error").innerHTML =
mostrarMensajeDeError(tipoDeformInput, formInput);
}
}
function mostrarMensajeDeError(tipoDeformInput, formInput) {
let mensaje = "";
tipoDeErrores.forEach((error) => {
if (formInput.validity[error]) {
mensaje = mensajesDeError[tipoDeformInput][error];
}
});
return mensaje;
}
const tipoDeErrores = [
"valueMissing",
"typeMismatch"
];
const mensajesDeError = {
name: {
valueMissing: "Ingresa Tu Nombre",
},
lastname: {
valueMissing: "Ingresa Tu Apellido",
},
email: {
valueMissing: "Ingresa Tu Correo Electrónico",
typeMismatch: "El Correo Electrónico no es válido",
},
subject: {
valueMissing: "Ingresa un Asunto a tratar",
},
message: {
valueMissing: "Por favor escribe un breve mensaje",
}
}