forked from marquez.juan/clase-10-ejercicios-de-repaso
ejercicio 8: carrito de compras
This commit is contained in:
@@ -1 +1,61 @@
|
||||
// Agregar aquí el código javascript
|
||||
const formulario = document.getElementById('formulario');
|
||||
const nombreInput = document.getElementById('nombre');
|
||||
const edadInput = document.getElementById('edad');
|
||||
const contrasenaInput = document.getElementById('contrasena');
|
||||
|
||||
const errorNombre = document.getElementById('error-nombre');
|
||||
const errorEdad = document.getElementById('error-edad');
|
||||
const errorContrasena = document.getElementById('error-contrasena');
|
||||
const mensajeExito = document.getElementById('mensaje-exito');
|
||||
|
||||
function limpiarErrores() {
|
||||
errorNombre.textContent = '';
|
||||
errorEdad.textContent = '';
|
||||
errorContrasena.textContent = '';
|
||||
mensajeExito.textContent = '';
|
||||
}
|
||||
|
||||
function validarFormulario() {
|
||||
limpiarErrores();
|
||||
|
||||
let esValido = true;
|
||||
const nombre = nombreInput.value.trim();
|
||||
const edad = edadInput.value.trim();
|
||||
const contrasena = contrasenaInput.value;
|
||||
|
||||
if (nombre === '') {
|
||||
errorNombre.textContent = 'El nombre no puede quedar vacío.';
|
||||
esValido = false;
|
||||
}
|
||||
|
||||
if (edad === '') {
|
||||
errorEdad.textContent = 'La edad no puede quedar vacía.';
|
||||
esValido = false;
|
||||
} else {
|
||||
const edadNumero = Number(edad);
|
||||
if (!Number.isInteger(edadNumero) || edadNumero < 0 || edadNumero > 100) {
|
||||
errorEdad.textContent = 'La edad debe ser un número entero entre 0 y 100.';
|
||||
esValido = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (contrasena === '') {
|
||||
errorContrasena.textContent = 'La contraseña no puede quedar vacía.';
|
||||
esValido = false;
|
||||
} else if (contrasena.length < 8) {
|
||||
errorContrasena.textContent = 'La contraseña debe tener al menos 8 caracteres.';
|
||||
esValido = false;
|
||||
}
|
||||
|
||||
return esValido;
|
||||
}
|
||||
|
||||
formulario.addEventListener('submit', (event) => {
|
||||
event.preventDefault();
|
||||
|
||||
if (validarFormulario()) {
|
||||
mensajeExito.textContent = 'Formulario enviado correctamente.';
|
||||
} else {
|
||||
mensajeExito.textContent = '';
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user