-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
111 lines (86 loc) · 2.9 KB
/
app.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
// variables
const email = document.getElementById('email');
const asunto = document.getElementById('asunto');
const mensaje = document.getElementById('mensaje');
const btnEnviar = document.getElementById('enviar');
const formularioEnviar = document.getElementById('enviar-mail');
const resetBtn = document.getElementById('resetBtn');
// event Listener
eventListeners();
function eventListeners() {
// Inicio de la aplicación y deshabilitar submit
document.addEventListener('DOMContentLoaded', inicioApp);
// Campos del formulario
email.addEventListener('blur', validarCampo);
asunto.addEventListener('blur', validarCampo);
mensaje.addEventListener('blur', validarCampo);
// Boton de enviar en el submit
formularioEnviar.addEventListener('submit', enviarEmail);
// Boton de reset
resetBtn.addEventListener('click', resetFormulario);
}
// funciones
function inicioApp() {
// deshabilitar el envio
btnEnviar.disabled = true;
}
// Valida que el campo tengo algo escrito
function validarCampo() {
// Se valida la longitud del texto y que no este vacio
validarLongitud(this);
// Validar unicamente el email
if(this.type === 'email') {
validarEmail(this);
}
let errores = document.querySelectorAll('.error');
if(email.value !== '' && asunto.value !== '' && mensaje.value !== '' ) {
if(errores.length === 0) {
btnEnviar.disabled = false;
}
}
}
// Resetear el formulario
function resetFormulario(e) {
formularioEnviar.reset();
e.preventDefault();
}
// Cuando se envia el correo
function enviarEmail(e) {
// Spinner al presionar Enviar
const spinnerGif = document.querySelector('#spinner');
spinnerGif.style.display = 'block';
// Gif que envia email
const enviado = document.createElement('img');
enviado.src = 'img/mail.gif';
enviado.style.display = 'block';
// Ocultar Spinner y mostrar gif de enviado
setTimeout(function() {
spinnerGif.style.display = 'none';
document.querySelector('#loaders').appendChild( enviado );
setTimeout(function() {
enviado.remove();
formularioEnviar.reset();
}, 5000);
}, 3000);
e.preventDefault();
}
// Verifica la longitud del texto en los campos
function validarLongitud(campo) {
if(campo.value.length > 0 ) {
campo.style.borderBottomColor = 'green';
campo.classList.remove('error');
} else {
campo.style.borderBottomColor = 'red';
campo.classList.add('error');
}
}
function validarEmail(campo) {
const mensaje = campo.value;
if(mensaje.indexOf('@') !== -1 ) {
campo.style.borderBottomColor = 'green';
campo.classList.remove('error');
} else {
campo.style.borderBottomColor = 'red';
campo.classList.add('error');
}
}