Skip to content

Commit eb16726

Browse files
committed
feat: Trim and validate chat form inputs before submission
- Trim whitespace from sender and message inputs - Prevent empty submissions and show alert if fields are blank - Return early from submit handler when validation fails - Improve UX and prevent sending invalid messages
1 parent 07982a1 commit eb16726

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

chat-app/frontend/script.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,16 @@ const messageElement = document.getElementById("chat-message");
7474
formElement.addEventListener("submit", async (event) => {
7575
event.preventDefault();
7676

77-
const senderValue = senderElement.value;
78-
const messageValue = messageElement.value;
77+
// Get the values and trim them
78+
const senderValue = senderElement.value.trim();
79+
const messageValue = messageElement.value.trim();
80+
81+
// The validation
82+
if (senderValue === "" || messageValue === "") {
83+
alert("Please enter both a name and a message!");
84+
85+
return;
86+
}
7987

8088
try {
8189
// Send the data

0 commit comments

Comments
 (0)