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 = '';
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,2 +1,42 @@
|
||||
/* Agregar el código CSS necesario para el ejercicio */
|
||||
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
margin: 20px;
|
||||
}
|
||||
|
||||
.campo {
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
input {
|
||||
display: block;
|
||||
width: 100%;
|
||||
max-width: 320px;
|
||||
padding: 8px;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
button {
|
||||
padding: 10px 16px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.error {
|
||||
display: block;
|
||||
margin-top: 4px;
|
||||
color: #c0392b;
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.exito {
|
||||
margin-top: 16px;
|
||||
color: #2d7a2d;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,13 +2,37 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Ejercicio 7</title>
|
||||
<link rel="stylesheet" href="estilo.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Ejercicio 7</h1>
|
||||
|
||||
<form id="formulario">
|
||||
<div class="campo">
|
||||
<label for="nombre">Nombre:</label>
|
||||
<input id="nombre" name="nombre" type="text">
|
||||
<span class="error" id="error-nombre"></span>
|
||||
</div>
|
||||
|
||||
<div class="campo">
|
||||
<label for="edad">Edad:</label>
|
||||
<input id="edad" name="edad" type="number" min="0" max="100">
|
||||
<span class="error" id="error-edad"></span>
|
||||
</div>
|
||||
|
||||
<div class="campo">
|
||||
<label for="contrasena">Contraseña:</label>
|
||||
<input id="contrasena" name="contrasena" type="password">
|
||||
<span class="error" id="error-contrasena"></span>
|
||||
</div>
|
||||
|
||||
<button type="submit">Enviar</button>
|
||||
</form>
|
||||
|
||||
<p id="mensaje-exito" class="exito" aria-live="polite"></p>
|
||||
|
||||
<script src="ejercicio7.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user