Ejercicio 5

This commit is contained in:
Bruno Dalessandro
2026-05-27 15:41:49 -03:00
parent 248c29b43b
commit 8a9b565164
2 changed files with 91 additions and 3 deletions

View File

@@ -141,4 +141,79 @@ botonCap.addEventListener("click", () => {
```js
document.querySelector("#nombre").value = "";
```
*/
*/
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 = "";
}
});