Skip to content

Commit

Permalink
Set description limit for new list form, both front end and backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Suvadip-sana committed Nov 10, 2024
1 parent 5b10e6d commit 6110cd0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
6 changes: 6 additions & 0 deletions controllers/listing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
42 changes: 41 additions & 1 deletion public/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,44 @@ listingImageInput.addEventListener('change', function() {
fileError.classList.remove("alert-error");
fileError.classList.add("nomal-error");
}
});
});



// // 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");
}
});
3 changes: 2 additions & 1 deletion views/new.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@
<div class="mb-3 listing_form">
<label for="description" class="form-label">Description</label>
<br>
<textarea name="listing[description]" class="form-control" cols="30" rows="5" placeholder="Enter description....." required></textarea>
<textarea name="listing[description]" id="list-description" class="form-control" cols="30" rows="5" placeholder="Enter description....." required></textarea>
<small class="" id="des-error"></small>
<div class="invalid-feedback">Please add a description!</div>
<div class="valid-feedback">Looks good!</div>
</div>
Expand Down

0 comments on commit 6110cd0

Please sign in to comment.