From d347427c7f27e4d43c452a4e592336f673460687 Mon Sep 17 00:00:00 2001 From: Sergio Madera De Marco <37299705@terciariourquiza.edu.ar> Date: Sun, 24 May 2026 14:58:33 -0300 Subject: [PATCH] ejercicio 5: formulario con validaciones y eventos --- clase-9.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ estilo.css | 12 +++++++++++ index.html | 20 ++++++++++++++++++- 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/clase-9.js b/clase-9.js index 814733f..453d444 100644 --- a/clase-9.js +++ b/clase-9.js @@ -51,3 +51,61 @@ botonCaptura.addEventListener('click', function() { externo.addEventListener('click', function() { console.log('capturing: externo'); }, true); + +// Ejercicio 5 - Validación de formulario +const formulario = document.getElementById('formulario'); +const nombreInput = document.getElementById('nombre'); +const edadInput = document.getElementById('edad'); +const mensajeInput = document.getElementById('mensaje'); +const errorNombre = document.getElementById('errorNombre'); +const errorEdad = document.getElementById('errorEdad'); +const errorMensaje = document.getElementById('errorMensaje'); +const formResultado = document.getElementById('formResultado'); + +function limpiarErrores() { + errorNombre.textContent = ''; + errorEdad.textContent = ''; + errorMensaje.textContent = ''; + formResultado.textContent = ''; +} + +formulario.addEventListener('submit', function(event) { + event.preventDefault(); + limpiarErrores(); + + let esValido = true; + const nombre = nombreInput.value.trim(); + const edadValor = edadInput.value.trim(); + const mensaje = mensajeInput.value.trim(); + + if (!nombre) { + errorNombre.textContent = 'El nombre no puede estar vacío.'; + esValido = false; + } + + if (!edadValor) { + errorEdad.textContent = 'La edad no puede estar vacía.'; + esValido = false; + } else if (!/^[0-9]+$/.test(edadValor)) { + errorEdad.textContent = 'La edad debe ser un número entero positivo.'; + esValido = false; + } else { + const edad = parseInt(edadValor, 10); + if (edad <= 0 || edad >= 120) { + errorEdad.textContent = 'La edad debe ser mayor que 0 y menor que 120.'; + esValido = false; + } + } + + if (!mensaje) { + errorMensaje.textContent = 'El mensaje no puede estar vacío.'; + esValido = false; + } + + if (esValido) { + formResultado.textContent = 'Formulario enviado con éxito.'; + nombreInput.value = ''; + edadInput.value = ''; + mensajeInput.value = ''; + } +}); diff --git a/estilo.css b/estilo.css index f4f6680..5bb4148 100644 --- a/estilo.css +++ b/estilo.css @@ -21,3 +21,15 @@ div { color: #ffffff; } +.error { + color: #b30000; + font-size: 0.9em; + display: block; + margin-top: 4px; +} + +.success { + color: #0a6f0a; + font-weight: bold; + margin-top: 10px; +} diff --git a/index.html b/index.html index a8c0853..fb71c61 100644 --- a/index.html +++ b/index.html @@ -38,7 +38,25 @@