diff --git a/clase-9.js b/clase-9.js index 684cd65..5844ae5 100644 --- a/clase-9.js +++ b/clase-9.js @@ -141,4 +141,79 @@ botonCap.addEventListener("click", () => { ```js document.querySelector("#nombre").value = ""; ``` -*/ \ No newline at end of file +*/ + + + +function validarNombre() { + + const nombre = document.getElementById("nombre").value; + if (nombre.trim() === "") { + document.getElementById("errorNombre").textContent="El campo nombre no puede estar vacio"; + return false; + } + + document.getElementById("errorNombre").textContent = ""; + return true; + +} + +function validarEdad() { + + const edadTexto = document.getElementById("edad").value; + + if (edadTexto.trim() === "") { + + document.getElementById("errorEdad").textContent = "Escribir la edad"; + return false; + } + + const edad = parseInt(edadTexto); + + if (isNaN(edad)) { + document.getElementById("errorEdad").textContent = "La edad no puede ser un texto"; + return false; + } + if (edad <= 0 || edad >= 120) { + document.getElementById("errorEdad").textContent = "Edad invalida"; + return false; + } + document.getElementById("errorEdad").textContent = ""; + return true; + +} + +function validarMensaje () { + + const mensaje = document.getElementById("mensaje").value; + + if (mensaje.trim() === "") { + + document.getElementById("errorMensaje").textContent = "El mensaje no puede estar vacio"; + return false; + } + + document.getElementById("errorMensaje").textContent = ""; + return true; + +} + +const form = document.querySelector("#formulario") +form.addEventListener("submit", (evento) => { + + evento.preventDefault(); + + const nombreValido = validarNombre(); + const edadValida = validarEdad(); + const mensajeValido = validarMensaje(); + + if (nombreValido && edadValida && mensajeValido) { + + document.getElementById("exito").textContent = "Formulario enviado correctamente"; + document.getElementById("nombre").value = ""; + document.getElementById("edad").value = ""; + document.getElementById("mensaje").value = ""; + + } + +}); \ No newline at end of file diff --git a/index.html b/index.html index 8688bfb..d284de2 100644 --- a/index.html +++ b/index.html @@ -46,8 +46,21 @@