@Eileenpk
Posted
Hi Godstime! your project looks good.
Forms were hard for me too when I started, and validation can get messy. Here are a few things I see that might help.
In your HTML:
- Add an aria-label to the email input for accessibility
<input type="email" name="email" id="email" placeholder="Email Address" aria-label="Email Address">
In your JS:
- Change the event listener to use input event instead of click, this will fire every time the input field changes
- Add or remove classes instead of manipulating them
- Check for an empty input before checking for a pattern match, currently if I try to submit the form with an empty email input, no error is shown
email.addEventListener('input', validateEmail);
function validateEmail() {
if (email.value.trim() === '') {
paragraph.innerHTML = 'Please provide an email';
email.classList.remove('bg-form');
error.src = '';
} else if (!email.value.match(patterns)) {
paragraph.innerHTML = 'Please provide a valid email';
email.classList.add('bg-form');
error.src = '../images/icon-error.svg';
} else {
paragraph.innerHTML = 'Please submit the email';
email.classList.remove('bg-form');
error.src = '';
}
}
Hope you found this helpful!
- Let's connect on LinkedIn! - @Eileenpk
Marked as helpful
@iceberg61
Posted
@Eileenpk You literally just explain what i have been busting my ass off in literally 2 minutesππππ. Thanks alot. it Was very helpful. Much love from Godstime