forked from marquez.juan/clase-11-ejercicio-integrador
feat: add validation for book input and alert on invalid inputs
This commit is contained in:
31
script.js
31
script.js
@@ -33,5 +33,36 @@ const renderTable = (books) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
const validateForm = (tittle, author, year, score) => {
|
||||||
|
if (!tittle || !author || !year || !score) return false;
|
||||||
|
if (year < 0 || score < 0 || score > 10) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const addBook = (tittle, author, year, score) => {
|
||||||
|
if(!validateForm(tittle, author, year, score)) {
|
||||||
|
alert('Datos invalidos');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
books.push({tittle: tittle, year: Number(year), score: Number(score), author: author});
|
||||||
|
renderTable(books);
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById('boton-agregar').addEventListener('click', () => {
|
||||||
|
|
||||||
|
const tittle = document.getElementById('titulo').value;
|
||||||
|
const author = document.getElementById('autor').value;
|
||||||
|
const year = document.getElementById('anio').value;
|
||||||
|
const score = document.getElementById('calificacion').value;
|
||||||
|
|
||||||
|
addBook(tittle, author, year, score);
|
||||||
|
|
||||||
|
document.getElementById('titulo').value = "";
|
||||||
|
document.getElementById('autor').value = "";
|
||||||
|
document.getElementById('anio').value = "";
|
||||||
|
document.getElementById('calificacion').value = "";
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
renderTable(books);
|
renderTable(books);
|
||||||
Reference in New Issue
Block a user