Skip to content

Commit c98b896

Browse files
Darkidd77Copilot
andcommitted
Improve form input handling and validation in book submission
Co-authored-by: Copilot <copilot@github.com>
1 parent dd1846f commit c98b896

4 files changed

Lines changed: 31 additions & 33 deletions

File tree

Project-TV-Show

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 48745b1055393956b0578f73a729c1e059bf6bc6

Project-TV-Show-Mahmoud

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit b47305bdd011fa8aa52f6adea17269b4fcde74f5

debugging/book-library/index.html

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ <h1>Library</h1>
3434
name="title"
3535
required
3636
/>
37-
<label for="author">Author: </label>
37+
<label for="author">Author:</label>
3838
<input
3939
type="text"
4040
class="form-control"
@@ -49,8 +49,9 @@ <h1>Library</h1>
4949
id="pages"
5050
name="pages"
5151
required
52+
min="1"
5253
/>
53-
<label class="form-check-label">
54+
<label for="check" class="form-check-label">
5455
<input
5556
type="checkbox"
5657
class="form-check-input"
@@ -77,17 +78,9 @@ <h1>Library</h1>
7778
<th></th>
7879
</tr>
7980
</thead>
80-
<tbody>
81-
<tr>
82-
<td></td>
83-
<td></td>
84-
<td></td>
85-
<td></td>
86-
<td></td>
87-
</tr>
88-
</tbody>
81+
<tbody></tbody>
8982
</table>
9083

91-
<script src="script.js"></script>
84+
<script src="script.js" type="module"></script>
9285
</body>
9386
</html>

debugging/book-library/script.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ let myLibrary = [];
22

33
window.addEventListener("load", function (e) {
44
populateStorage();
5-
render();
65
});
76

87
function populateStorage() {
@@ -20,35 +19,39 @@ function populateStorage() {
2019
}
2120
}
2221

23-
const title = document.getElementById("title");
24-
const author = document.getElementById("author");
25-
const pages = document.getElementById("pages");
26-
const check = document.getElementById("check");
22+
const titleInput = document.getElementById("title");
23+
const authorInput = document.getElementById("author");
24+
const pagesInput = document.getElementById("pages");
25+
const checkInput = document.getElementById("check");
2726

2827
//check the right input from forms and if its ok -> add the new book (object in array)
2928
//via Book function and start render function
3029
function submit() {
30+
const titleVal = titleInput.value.trim();
31+
const authorVal = authorInput.value.trim();
32+
const pagesVal = parseInt(pagesInput.value, 10);
33+
3134
if (
32-
title.value == null ||
33-
title.value == "" ||
34-
author.value == null ||
35-
author.value == "" ||
36-
pages.value == null ||
37-
pages.value == ""
35+
titleVal == null ||
36+
titleVal == "" ||
37+
authorVal == null ||
38+
authorVal == "" ||
39+
isNaN(pagesVal) ||
40+
pagesVal <= 0
3841
) {
3942
alert("Please fill all fields!");
4043
return false;
4144
} else {
42-
let book = new Book(title.value, author.value, pages.value, check.checked);
45+
let book = new Book(titleVal, authorVal, pagesVal, checkInput.checked);
4346
myLibrary.push(book);
4447
render();
4548
}
4649

4750
// clear the form after submit
48-
title.value = "";
49-
author.value = "";
50-
pages.value = "";
51-
check.checked = false;
51+
titleInput.value = "";
52+
authorInput.value = "";
53+
pagesInput.value = "";
54+
checkInput.checked = false;
5255
}
5356

5457
function Book(title, author, pages, check) {
@@ -74,9 +77,9 @@ function render() {
7477
let pagesCell = row.insertCell(2);
7578
let wasReadCell = row.insertCell(3);
7679
let deleteCell = row.insertCell(4);
77-
titleCell.innerHTML = myLibrary[i].title;
78-
authorCell.innerHTML = myLibrary[i].author;
79-
pagesCell.innerHTML = myLibrary[i].pages;
80+
titleCell.textContent = myLibrary[i].title;
81+
authorCell.textContent = myLibrary[i].author;
82+
pagesCell.textContent = myLibrary[i].pages;
8083

8184
//add and wait for action for read/unread button
8285
let changeBut = document.createElement("button");
@@ -89,7 +92,7 @@ function render() {
8992
} else {
9093
readStatus = "No";
9194
}
92-
changeBut.innerText = readStatus;
95+
changeBut.textContent = readStatus;
9396

9497
changeBut.addEventListener("click", function () {
9598
myLibrary[i].check = !myLibrary[i].check;
@@ -101,7 +104,7 @@ function render() {
101104
delButton.id = i + 5;
102105
deleteCell.appendChild(delButton);
103106
delButton.className = "btn btn-warning";
104-
delButton.innerHTML = "Delete";
107+
delButton.textContent = "Delete";
105108
delButton.addEventListener("click", function () {
106109
alert(`You've deleted title: ${myLibrary[i].title}`);
107110
myLibrary.splice(i, 1);

0 commit comments

Comments
 (0)