forked from marquez.juan/clase-10-ejercicios-de-repaso
feat: implement user form with validation for username, age, and password
This commit is contained in:
@@ -1 +1,24 @@
|
|||||||
// Agregar aquí el código javascript
|
// Agregar aquí el código javascript
|
||||||
|
|
||||||
|
const userForm = document.getElementById('userForm');
|
||||||
|
|
||||||
|
userForm.addEventListener('submit', (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
const username = document.getElementById('username').value.trim();
|
||||||
|
const age = parseInt(document.getElementById('age').value);
|
||||||
|
const password = document.getElementById('password').value;
|
||||||
|
if (username === '') {
|
||||||
|
alert('El nombre de usuario no puede estar vacío.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (isNaN(age) || age < 0 || age > 100) {
|
||||||
|
alert('La edad debe ser un número válido entre 0 y 100.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (password.length < 8) {
|
||||||
|
alert('La contraseña debe tener al menos 8 caracteres.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
alert('Formulario enviado correctamente.');
|
||||||
|
userForm.reset();
|
||||||
|
});
|
||||||
@@ -8,7 +8,15 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Ejercicio 7</h1>
|
<h1>Ejercicio 7</h1>
|
||||||
|
<form id="userForm">
|
||||||
|
<label for="username">Nombre de usuario:</label>
|
||||||
|
<input type="text" id="username" name="username" required>
|
||||||
|
<label for="age">Edad:</label>
|
||||||
|
<input type="number" id="age" name="age" required>
|
||||||
|
<label for="password">Contraseña:</label>
|
||||||
|
<input type="password" id="password" name="password" required>
|
||||||
|
<button type="submit">Enviar</button>
|
||||||
|
</form>
|
||||||
<script src="ejercicio7.js"></script>
|
<script src="ejercicio7.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user