-
Notifications
You must be signed in to change notification settings - Fork 0
/
validacion.js
76 lines (61 loc) · 2.26 KB
/
validacion.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
//Haz tú validación en javascript acá
document.addEventListener("DOMContentLoaded", () => {
const form = document.getElementById('formcontato__form');
let users = JSON.parse(localStorage.getItem("users")) || [];
form.addEventListener("submit", (event) => {
event.preventDefault();
const nombre = form.querySelector('input[name="nombre"]').value.trim();
const email = form.querySelector('input[name="email"]').value.trim();
const asunto = form.querySelector('input[name="asunto"]').value.trim();
if (nombre === "" ||
email === "" ||
asunto === "" ){
showError("Por favor, completa los campos obligatorios.");
return;
}
const user = {
nombre,
email,
asunto
};
users.push(user);
localStorage.setItem("users", JSON.stringify(users));
form.reset();
});
function showError(message) {
const error = document.querySelector(".error");
error.textContent = message;
setTimeout(() => {
error.textContent = "";
}, 2000);
}
});
function changeRadioButtonSelection(selectedRadioButton) {
const radioButtons = document.getElementsByName('radio');
const clickedIndex = Array.from(radioButtons).indexOf(selectedRadioButton);
const nextIndex = (selectedIndex + 1) % radioButtons.length;
radioButtons[nextIndex].checked = true;
};
// vvvvvvvvv Menú hamburguesa vvvvvvvvv
const BURGER_ID = document.getElementById("burger");
const CLOSE_NAV = document.getElementById("close-nav");
const OPEN_NAV = document.getElementById("myNav");
function hide(e) {
e.preventDefault();
OPEN_NAV.classList.toggle("hidden");
document.body.classList.toggle("no-scroll");
}
BURGER_ID.addEventListener("click", (e) => {
hide(e);
});
CLOSE_NAV.addEventListener("click", (e) => {
hide(e);
});
document.querySelectorAll("#myNav a").forEach(link => {
link.addEventListener("click", (e) => {
const targetSectionId = e.target.getAttribute('href').substring(1);
const targetSection = document.getElementById(targetSectionId);
targetSection.scrollIntoView({ behavior: 'smooth' });
hide(e);
});
});