@@ -21,42 +21,32 @@ const check = document.getElementById("check");
2121// Notification function
2222function showNotification ( message ) {
2323 let notification = document . getElementById ( "notification" ) ;
24- if ( ! notification ) {
25- notification = document . createElement ( "div" ) ;
26- notification . id = "notification" ;
27- notification . className = "notification" ;
28- document . body . insertBefore ( notification , document . body . firstChild ) ;
29- }
3024 notification . textContent = message ;
3125 notification . style . display = "block" ;
3226 setTimeout ( ( ) => {
3327 notification . style . display = "none" ;
3428 } , 3000 ) ; // 3 seconds
3529}
3630
31+ // Listen for form submission
32+ document . getElementById ( "bookForm" ) . addEventListener ( "submit" , function ( event ) {
33+ event . preventDefault ( ) ;
34+ submit ( ) ;
35+ } ) ;
36+
3737function submit ( ) {
3838 const titleValue = title . value . trim ( ) ;
3939 const authorValue = author . value . trim ( ) ;
4040 const pagesValue = pages . value . trim ( ) ;
4141
42- if ( ! titleValue || ! authorValue || ! pagesValue ) {
43- alert ( "Please fill all fields!" ) ;
44- return false ;
45- }
46-
47- // Prevent numbers in Author field
48- if ( ! / ^ [ a - z A - Z \s . ' - ] + $ / . test ( authorValue ) ) {
49- alert ( "Author name can only contain letters, spaces, apostrophes, periods, or hyphens." ) ;
50- return false ;
51- }
52- // pages must be a positive number
42+ // Additional validation for pages
5343 const pagesNumber = Number ( pagesValue ) ;
5444 if ( ! Number . isInteger ( pagesNumber ) || pagesNumber <= 0 ) {
55- alert ( "Number of pages must be a positive whole number." ) ;
56- return false ;
45+ showNotification ( "Number of pages must be a positive whole number." ) ;
46+ return ;
5747 }
5848
59- let book = new Book ( titleValue , authorValue , Number ( pagesValue ) , check . checked ) ;
49+ let book = new Book ( titleValue , authorValue , pagesNumber , check . checked ) ;
6050 myLibrary . push ( book ) ;
6151 render ( ) ;
6252
0 commit comments