-
Notifications
You must be signed in to change notification settings - Fork 1
/
validarForm.js
61 lines (55 loc) · 1.87 KB
/
validarForm.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
// Obtener valores de los campos
const nombre = document.getElementById("nombreInput");
const apellido = document.getElementById("apellidoInput");
const email = document.getElementById("emailInput");
const telefono = document.getElementById("telefonoInput");
const form = document.getElementById("form");
const mensajesErrores = document.getElementById("errores");
const consulta = document.getElementById("consultaInput")
//validacion de datos ingresados
form.addEventListener("submit", e => {
e.preventDefault();
let flag = false
let errores = "";
let validaEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
let validaTelefono = /^[0-9]{10}$/;
mensajesErrores.innerHTML = ""
if (nombre.value.length < 2) {
errores += `El nombre no es valido <br>`;
flag =true;
}
if (apellido.value.length < 2) {
errores += `El apellido no es valido <br>`;
flag =true;
}
if(!validaEmail.test(email.value)){
errores += `El Email no es valido <br>`;
flag = true;
}
if (!validaTelefono.test(telefono.value)) {
errores += `El telefono no es valido <br>`;
flag =true;
}
if (consulta.value.length < 10) {
errores += `Falta escribir su consulta <br>`;
flag =true;
}
//Dibuja en el DOM abajo el boton de submit los mensajes de
//errores o de form enviado correctamente
if(flag){
mensajesErrores.innerHTML = errores;
mensajesErrores.style.color = 'red'
mensajesErrores.style.fontWeight = 'bold'
}
else{
nombre.value = "";
apellido.value = "";
email.value = "";
telefono.value = "";
consulta.value = "";
mensajesErrores.innerHTML = "ENVIADO"
mensajesErrores.style.color = 'green'
mensajesErrores.style.fontWeight = 'bold'
alert("CONSULTA ENVIADA CON EXITO!")
}
})