feat: Add form validations and success message to contact form#9
Open
akshay0611 wants to merge 1 commit intocodewithsadee:masterfrom
Open
feat: Add form validations and success message to contact form#9akshay0611 wants to merge 1 commit intocodewithsadee:masterfrom
akshay0611 wants to merge 1 commit intocodewithsadee:masterfrom
Conversation
Imran-imtiaz48
left a comment
There was a problem hiding this comment.
The recent changes in the script.js file introduce a new event listener for handling the form submission of the contactForm. The code ensures form validation and provides user feedback through alerts and a success message. Additionally, it clears the form fields after a successful submission.
This implementation effectively enhances user experience and ensures better usability of the contact form. The use of setTimeout to hide the success message is a good addition, as it provides users with feedback without requiring manual dismissal.
Strengths
- Form Validation: The code validates inputs for
name,email,phone, andmessagefields and provides immediate feedback to the user. This ensures that the data entered is accurate before submission. - User Feedback: The success message gives clear feedback to the user upon successful submission.
- Code Clarity: The logic is clear and easy to follow, with validation checks and comments explaining each step.
Suggestions for Improvement
- Error Message Consistency: Consider using a consistent approach for error messages across all fields. For instance, while
emailusesvalidationMessage, other fields rely ontitle. UsingvalidationMessagefor all fields could improve consistency.alert(name.validationMessage || name.title);
- Accessibility: Ensure the success message is accessible for screen readers by adding appropriate ARIA roles or attributes (e.g.,
role="alert").<div class="success-message" role="alert" style="display: none;">Form submitted successfully!</div>
- Code Reusability: The validation logic could be refactored into a reusable function to reduce redundancy.
Then, use it like this:
function validateField(field, errorMessage) { if (!field.checkValidity()) { alert(errorMessage || field.validationMessage); return false; } return true; }
if (!validateField(name, 'Name is required')) return; if (!validateField(email, 'Please enter a valid email address')) return; if (!validateField(phone, 'Phone number is required')) return; if (!validateField(message, 'Message must be between 10 and 500 characters')) return;
- Clear Success Message Visibility: Instead of hiding the success message after exactly 5 seconds, allow the user to dismiss it manually as well (e.g., by adding a close button).
<div class="success-message" style="display: none;"> Form submitted successfully! <button onclick="this.parentElement.style.display='none'">Close</button> </div>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
This PR enhances the contact form by adding client-side validations and a success message. The changes aim to improve the user experience by ensuring valid inputs and providing feedback upon successful submission.
Changes Made:
pattern,minlength,maxlength) to enforce input constraints.Testing:
Related Issue:
Closes #8