From 6d1db61392e0cf9c67f39fc63855845ad32fa424 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Tue, 19 Aug 2025 23:04:14 +0100 Subject: [PATCH 01/21] remove --- debugging/book-library/index.html | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..e4fccb17 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,7 +1,7 @@ - + My Library Library
Library /> Library - - - - - - - + From 74a65bb4b15d42463a466c78cb1be2df097896d1 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Tue, 19 Aug 2025 23:07:40 +0100 Subject: [PATCH 02/21] submit and console error --- debugging/book-library/script.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..c94010ca 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -16,7 +16,6 @@ function populateStorage() { ); myLibrary.push(book1); myLibrary.push(book2); - render(); } } @@ -25,8 +24,7 @@ const author = document.getElementById("author"); const pages = document.getElementById("pages"); const check = document.getElementById("check"); -//check the right input from forms and if its ok -> add the new book (object in array) -//via Book function and start render function +// Check the right input from forms and add the new book function submit() { if ( title.value == null || @@ -37,8 +35,8 @@ function submit() { alert("Please fill all fields!"); return false; } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); + let book = new Book(title.value, author.value, pages.value, check.checked); // Use author.value correctly + myLibrary.push(book); // Fixed: push to myLibrary render(); } } @@ -53,11 +51,11 @@ function Book(title, author, pages, check) { function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; - //delete old table - for (let n = rowsNumber - 1; n > 0; n-- { + // Delete old table rows + for (let n = rowsNumber - 1; n > 0; n--) { table.deleteRow(n); } - //insert updated row and cells + // Insert updated rows let length = myLibrary.length; for (let i = 0; i < length; i++) { let row = table.insertRow(1); @@ -70,7 +68,7 @@ function render() { authorCell.innerHTML = myLibrary[i].author; pagesCell.innerHTML = myLibrary[i].pages; - //add and wait for action for read/unread button + // Add read/unread button let changeBut = document.createElement("button"); changeBut.id = i; changeBut.className = "btn btn-success"; @@ -88,13 +86,13 @@ function render() { render(); }); - //add delete button to every row and render again + // Add delete button let delButton = document.createElement("button"); - delBut.id = i + 5; - deleteCell.appendChild(delBut); - delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delButton.id = i + 5; + deleteCell.appendChild(delButton); + delButton.className = "btn btn-warning"; + delButton.innerHTML = "Delete"; + delButton.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From 87c782adadc642377c9c8be0968ef612e94b4a74 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Tue, 19 Aug 2025 23:09:27 +0100 Subject: [PATCH 03/21] title.value to author.value --- debugging/book-library/script.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index c94010ca..694a0501 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -35,8 +35,8 @@ function submit() { alert("Please fill all fields!"); return false; } else { - let book = new Book(title.value, author.value, pages.value, check.checked); // Use author.value correctly - myLibrary.push(book); // Fixed: push to myLibrary + let book = new Book(title.value, author.value, pages.value, check.checked); // Fixed: author.value + myLibrary.push(book); render(); } } From 13940dfab3f4e4b78de580f4aa0b5e01d10b6e93 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Tue, 19 Aug 2025 23:12:16 +0100 Subject: [PATCH 04/21] delbutton/clicks typo --- debugging/book-library/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 694a0501..067238c4 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -35,7 +35,7 @@ function submit() { alert("Please fill all fields!"); return false; } else { - let book = new Book(title.value, author.value, pages.value, check.checked); // Fixed: author.value + let book = new Book(title.value, author.value, pages.value, check.checked); myLibrary.push(book); render(); } From 87b518731dcd585dd3ffacbf66d9e338a0912d86 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Tue, 19 Aug 2025 23:15:01 +0100 Subject: [PATCH 05/21] render function condition --- debugging/book-library/script.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 067238c4..e6151475 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -73,8 +73,10 @@ function render() { changeBut.id = i; changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); + + // render function let readStatus = ""; - if (myLibrary[i].check == false) { + if (myLibrary[i].check === true) { readStatus = "Yes"; } else { readStatus = "No"; From 6b150ac94e0c08da5368d4df560edd9e75d3a2ed Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Tue, 19 Aug 2025 23:22:10 +0100 Subject: [PATCH 06/21] add validation author --- debugging/book-library/script.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index e6151475..538b0808 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -28,9 +28,11 @@ const check = document.getElementById("check"); function submit() { if ( title.value == null || - title.value == "" || + title.value.trim() === "" || + author.value == null || + author.value.trim() === "" || pages.value == null || - pages.value == "" + pages.value === "" ) { alert("Please fill all fields!"); return false; @@ -38,6 +40,11 @@ function submit() { let book = new Book(title.value, author.value, pages.value, check.checked); myLibrary.push(book); render(); + + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; } } From 5b00729c17820c71e6dccb1f70bd313637bd0bd7 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Tue, 19 Aug 2025 23:52:55 +0100 Subject: [PATCH 07/21] add clickable icon --- debugging/book-library/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index e4fccb17..388dc688 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -71,7 +71,7 @@

Library

- + From 6fc08fe494b251c0ed718b32482f069eb75c6a66 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Tue, 19 Aug 2025 23:53:40 +0100 Subject: [PATCH 08/21] author number validation --- debugging/book-library/script.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 538b0808..b7ab5cba 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -36,16 +36,22 @@ function submit() { ) { alert("Please fill all fields!"); return false; - } else { - let book = new Book(title.value, author.value, pages.value, check.checked); - myLibrary.push(book); - render(); + } - title.value = ""; - author.value = ""; - pages.value = ""; - check.checked = false; + // Prevent numbers in Author field + if (/\d/.test(author.value)) { + alert("Author name cannot contain numbers!"); + return false; } + + let book = new Book(title.value, author.value, pages.value, check.checked); + myLibrary.push(book); + render(); + + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; } function Book(title, author, pages, check) { @@ -81,7 +87,6 @@ function render() { changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); - // render function let readStatus = ""; if (myLibrary[i].check === true) { readStatus = "Yes"; @@ -108,3 +113,8 @@ function render() { }); } } + +// Make header row toggle the form +document.querySelector(".thead-dark").addEventListener("click", function () { + $("#demo").collapse("toggle"); +}); From 7ec7ebf4fc268d111b4a82934d263adff96a8b5f Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Tue, 19 Aug 2025 23:53:56 +0100 Subject: [PATCH 09/21] pointer cursor --- debugging/book-library/style.css | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..8857fe17 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -17,3 +17,7 @@ button.btn-info { margin: 20px; } + +.clickable { + cursor: pointer; +} From ee53951fa6f63a62a9f412ef792eea2f774083f2 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 20 Aug 2025 00:22:25 +0100 Subject: [PATCH 10/21] fix thead --- debugging/book-library/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 388dc688..f6438838 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -71,8 +71,8 @@

Library

Title Author
- - + + From 504539093e5330b25fab1d3a24f7660a7069486a Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 20 Aug 2025 00:23:44 +0100 Subject: [PATCH 11/21] remove clickable row --- debugging/book-library/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index b7ab5cba..31a28ae9 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -115,6 +115,6 @@ function render() { } // Make header row toggle the form -document.querySelector(".thead-dark").addEventListener("click", function () { +document.querySelector(".thead-dark tr").addEventListener("click", function (e) { $("#demo").collapse("toggle"); }); From f45f0e4e3fa8289b85b06ea0fe71ab6e90abb935 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 20 Aug 2025 09:02:29 +0100 Subject: [PATCH 12/21] remove render() --- debugging/book-library/script.js | 1 - 1 file changed, 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 31a28ae9..e1bfdb13 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -2,7 +2,6 @@ let myLibrary = []; window.addEventListener("load", function (e) { populateStorage(); - render(); }); function populateStorage() { From 76bf57cdd0305ef21804a90de6c1dc45eab77316 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 20 Aug 2025 09:58:08 +0100 Subject: [PATCH 13/21] debug validations submit(), render() --- debugging/book-library/script.js | 116 +++++++++++++------------------ 1 file changed, 48 insertions(+), 68 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index e1bfdb13..86b4ab31 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,20 +1,15 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", function () { populateStorage(); + render(); }); function populateStorage() { - if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); - let book2 = new Book( - "The Old Man and the Sea", - "Ernest Hemingway", - "127", - true - ); - myLibrary.push(book1); - myLibrary.push(book2); + if (myLibrary.length === 0) { + let book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true); + let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true); + myLibrary.push(book1, book2); } } @@ -23,30 +18,27 @@ const author = document.getElementById("author"); const pages = document.getElementById("pages"); const check = document.getElementById("check"); -// Check the right input from forms and add the new book function submit() { - if ( - title.value == null || - title.value.trim() === "" || - author.value == null || - author.value.trim() === "" || - pages.value == null || - pages.value === "" - ) { + const titleValue = title.value.trim(); + const authorValue = author.value.trim(); + const pagesValue = pages.value.trim(); + + if (!titleValue || !authorValue || !pagesValue) { alert("Please fill all fields!"); return false; } // Prevent numbers in Author field - if (/\d/.test(author.value)) { + if (/\d/.test(authorValue)) { alert("Author name cannot contain numbers!"); return false; } - let book = new Book(title.value, author.value, pages.value, check.checked); + let book = new Book(titleValue, authorValue, Number(pagesValue), check.checked); myLibrary.push(book); render(); + // Reset form title.value = ""; author.value = ""; pages.value = ""; @@ -61,59 +53,47 @@ function Book(title, author, pages, check) { } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; - // Delete old table rows - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); - } - // Insert updated rows - let length = myLibrary.length; - for (let i = 0; i < length; i++) { - let row = table.insertRow(1); - let titleCell = row.insertCell(0); - let authorCell = row.insertCell(1); - let pagesCell = row.insertCell(2); - let wasReadCell = row.insertCell(3); - let deleteCell = row.insertCell(4); - titleCell.innerHTML = myLibrary[i].title; - authorCell.innerHTML = myLibrary[i].author; - pagesCell.innerHTML = myLibrary[i].pages; - - // Add read/unread button - let changeBut = document.createElement("button"); - changeBut.id = i; - changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); - - let readStatus = ""; - if (myLibrary[i].check === true) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; - - changeBut.addEventListener("click", function () { + const table = document.getElementById("display"); + const tbody = table.querySelector("tbody"); + + // Clear all existing rows + tbody.innerHTML = ""; + + myLibrary.forEach((book, i) => { + const row = tbody.insertRow(); + + row.insertCell(0).textContent = book.title; + row.insertCell(1).textContent = book.author; + row.insertCell(2).textContent = book.pages; + + // Read/unread button + const wasReadCell = row.insertCell(3); + const readToggleButton = document.createElement("button"); + readToggleButton.className = "btn btn-success"; + readToggleButton.textContent = book.check ? "Yes" : "No"; + wasReadCell.appendChild(readToggleButton); + + readToggleButton.addEventListener("click", () => { myLibrary[i].check = !myLibrary[i].check; render(); }); - // Add delete button - let delButton = document.createElement("button"); - delButton.id = i + 5; - deleteCell.appendChild(delButton); - delButton.className = "btn btn-warning"; - delButton.innerHTML = "Delete"; - delButton.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); - myLibrary.splice(i, 1); - render(); + // Delete button + const deleteCell = row.insertCell(4); + const deleteButton = document.createElement("button"); + deleteButton.className = "btn btn-warning"; + deleteButton.textContent = "Delete"; + deleteCell.appendChild(deleteButton); + + deleteButton.addEventListener("click", () => { + myLibrary.splice(i, 1); // delete immediately + render(); // update the table + alert(`Deleted "${book.title}" successfully.`); // optional confirmation }); - } + }); } // Make header row toggle the form -document.querySelector(".thead-dark tr").addEventListener("click", function (e) { +document.querySelector(".thead-dark tr").addEventListener("click", function () { $("#demo").collapse("toggle"); }); From 4476c4357a05444377b515f693b5dddd366879c6 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 20 Aug 2025 09:58:35 +0100 Subject: [PATCH 14/21] remove name, content --- debugging/book-library/index.html | 2 -- 1 file changed, 2 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index f6438838..01b47bd3 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -4,8 +4,6 @@ My Library From 714badbbf93d5d5f8284099e8b745c5238238e6c Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 20 Aug 2025 10:05:41 +0100 Subject: [PATCH 15/21] remove read toggle --- debugging/book-library/script.js | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 86b4ab31..334e11d8 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -66,17 +66,8 @@ function render() { row.insertCell(1).textContent = book.author; row.insertCell(2).textContent = book.pages; - // Read/unread button - const wasReadCell = row.insertCell(3); - const readToggleButton = document.createElement("button"); - readToggleButton.className = "btn btn-success"; - readToggleButton.textContent = book.check ? "Yes" : "No"; - wasReadCell.appendChild(readToggleButton); - - readToggleButton.addEventListener("click", () => { - myLibrary[i].check = !myLibrary[i].check; - render(); - }); + // Read column (static) + row.insertCell(3).textContent = book.check ? "Yes" : "No"; // Delete button const deleteCell = row.insertCell(4); From f29e2c0df940109fe91efe3775251cb7a623ccda Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 20 Aug 2025 10:15:02 +0100 Subject: [PATCH 16/21] alert --- debugging/book-library/script.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 334e11d8..86b4ab31 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -66,8 +66,17 @@ function render() { row.insertCell(1).textContent = book.author; row.insertCell(2).textContent = book.pages; - // Read column (static) - row.insertCell(3).textContent = book.check ? "Yes" : "No"; + // Read/unread button + const wasReadCell = row.insertCell(3); + const readToggleButton = document.createElement("button"); + readToggleButton.className = "btn btn-success"; + readToggleButton.textContent = book.check ? "Yes" : "No"; + wasReadCell.appendChild(readToggleButton); + + readToggleButton.addEventListener("click", () => { + myLibrary[i].check = !myLibrary[i].check; + render(); + }); // Delete button const deleteCell = row.insertCell(4); From 6547b3264598c697da1692ac6765a7ca232d21db Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 20 Aug 2025 12:39:47 +0100 Subject: [PATCH 17/21] create form and require min 1 --- debugging/book-library/index.html | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 01b47bd3..238b99de 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -17,6 +17,7 @@
+

Library

Add books to your virtual library

@@ -26,6 +27,7 @@

Library

+
Library class="form-control" id="pages" name="pages" - required + required min="1" />
+
Title Author Number of Pages
From cff73c94ccfc9608263b453d56b6b8895534f7db Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 20 Aug 2025 12:40:02 +0100 Subject: [PATCH 18/21] remove alert --- debugging/book-library/script.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 86b4ab31..86cdedd5 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -18,6 +18,22 @@ const author = document.getElementById("author"); const pages = document.getElementById("pages"); const check = document.getElementById("check"); +// Notification function +function showNotification(message) { + let notification = document.getElementById("notification"); + if (!notification) { + notification = document.createElement("div"); + notification.id = "notification"; + notification.className = "notification"; + document.body.insertBefore(notification, document.body.firstChild); + } + notification.textContent = message; + notification.style.display = "block"; + setTimeout(() => { + notification.style.display = "none"; + }, 3000); // 3 seconds +} + function submit() { const titleValue = title.value.trim(); const authorValue = author.value.trim(); @@ -29,8 +45,14 @@ function submit() { } // Prevent numbers in Author field - if (/\d/.test(authorValue)) { - alert("Author name cannot contain numbers!"); + if (!/^[a-zA-Z\s.'-]+$/.test(authorValue)) { + alert("Author name can only contain letters, spaces, apostrophes, periods, or hyphens."); + return false; + } + // pages must be a positive number + const pagesNumber = Number(pagesValue); + if (!Number.isInteger(pagesNumber) || pagesNumber <= 0) { + alert("Number of pages must be a positive whole number."); return false; } @@ -88,7 +110,7 @@ function render() { deleteButton.addEventListener("click", () => { myLibrary.splice(i, 1); // delete immediately render(); // update the table - alert(`Deleted "${book.title}" successfully.`); // optional confirmation + showNotification(`Deleted "${book.title}" successfully.`); }); }); } From 85d011f280067125bc0d345b52c47f6188738320 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 20 Aug 2025 12:40:17 +0100 Subject: [PATCH 19/21] notification style --- debugging/book-library/style.css | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 8857fe17..d26eb05b 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -21,3 +21,20 @@ button.btn-info { .clickable { cursor: pointer; } +.notification { + display: none; + position: fixed; + top: 230px; + left: 50%; + transform: translateX(-50%); + padding: 15px 20px; + background-color: #f8d7da; + color: #721c24; + border: 1px solid #f5c6cb; + border-radius: 5px; + text-align: center; + font-weight: bold; + font-size: 1.2rem; + z-index: 1000; +} + From 43852c52c18453be871a70f94d3b568704657633 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 20 Aug 2025 12:55:11 +0100 Subject: [PATCH 20/21] auto validation --- debugging/book-library/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 238b99de..d705c4d1 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -44,6 +44,8 @@

Library

id="author" name="author" required + pattern="[a-zA-Z\s.'-]+" + title="Only letters, spaces, apostrophes, periods, or hyphens allowed" /> Library type="checkbox" class="form-check-input" id="check" - value="" />Read From e3d4f0ee2f2ebde6c37e32c4d0fb2f15d9b2e8a2 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 20 Aug 2025 12:55:45 +0100 Subject: [PATCH 21/21] remove validation check --- debugging/book-library/script.js | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 86cdedd5..17066062 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -21,12 +21,6 @@ const check = document.getElementById("check"); // Notification function function showNotification(message) { let notification = document.getElementById("notification"); - if (!notification) { - notification = document.createElement("div"); - notification.id = "notification"; - notification.className = "notification"; - document.body.insertBefore(notification, document.body.firstChild); - } notification.textContent = message; notification.style.display = "block"; setTimeout(() => { @@ -34,29 +28,25 @@ function showNotification(message) { }, 3000); // 3 seconds } +// Listen for form submission +document.getElementById("bookForm").addEventListener("submit", function(event) { + event.preventDefault(); + submit(); +}); + function submit() { const titleValue = title.value.trim(); const authorValue = author.value.trim(); const pagesValue = pages.value.trim(); - if (!titleValue || !authorValue || !pagesValue) { - alert("Please fill all fields!"); - return false; - } - - // Prevent numbers in Author field - if (!/^[a-zA-Z\s.'-]+$/.test(authorValue)) { - alert("Author name can only contain letters, spaces, apostrophes, periods, or hyphens."); - return false; - } - // pages must be a positive number + // Additional validation for pages const pagesNumber = Number(pagesValue); if (!Number.isInteger(pagesNumber) || pagesNumber <= 0) { - alert("Number of pages must be a positive whole number."); - return false; + showNotification("Number of pages must be a positive whole number."); + return; } - let book = new Book(titleValue, authorValue, Number(pagesValue), check.checked); + let book = new Book(titleValue, authorValue, pagesNumber, check.checked); myLibrary.push(book); render();