diff --git a/controllers/listing.js b/controllers/listing.js index 21e67727..0e9da2bc 100644 --- a/controllers/listing.js +++ b/controllers/listing.js @@ -98,6 +98,12 @@ module.exports.search = async (req, res) => { // Destructure the properties from req.body.listing, including tags const { title, description, price, country, location, tags } = req.body.listing; + //Description limit + if(description.length > 1000){ + req.flash("error", "Maximum 1000 charecters allowed!"); + return res.redirect("/listing/new"); + } + // Ensure tags is an array (if it's a comma-separated string, split it) let tagArray = []; if (tags) { diff --git a/public/js/script.js b/public/js/script.js index a0d8e020..c2139574 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -172,4 +172,44 @@ listingImageInput.addEventListener('change', function() { fileError.classList.remove("alert-error"); fileError.classList.add("nomal-error"); } -}); \ No newline at end of file +}); + + + +// // Set the descriptiopn limit +// const description = document.querySelector("#list-description"); +// const desError = document.querySelector("#des-error"); +// description.addEventListener("change", function(){ +// if(description.value.length >= 1000){ +// desError.classList.remove("nomal-error"); +// desError.classList.add("alert-error"); +// description.value = ""; +// } +// else{ +// desError.classList.remove("alert-error"); +// desError.classList.add("nomal-error"); +// } +// }) + + + +// Set the description limit +const description = document.querySelector("#list-description"); +const desError = document.querySelector("#des-error"); +const maxChars = 1000; + +description.addEventListener("input", function() { + const currentLength = description.value.length; + + if (currentLength > maxChars) { + // Limit the description to the maximum allowed characters + description.value = description.value.substring(0, maxChars); + desError.textContent = "You have reached the 1000-character limit!"; + desError.classList.add("alert-error"); + desError.classList.remove("normal-error"); + } else { + desError.textContent = `Maximum ${maxChars} characters!`; + desError.classList.remove("alert-error"); + desError.classList.add("normal-error"); + } +}); diff --git a/views/new.ejs b/views/new.ejs index 9a771e76..4bd16169 100644 --- a/views/new.ejs +++ b/views/new.ejs @@ -115,7 +115,8 @@