This is a solution to the Intro component with sign up form challenge on Frontend Mentor. Frontend Mentor challenges help you improve your coding skills by building realistic projects.
Users should be able to:
- View the optimal layout for the site depending on their device's screen size
- See hover states for all interactive elements on the page
- Receive an error message when the
form
is submitted if:- Any
input
field is empty. The message for this error should say "[Field Name] cannot be empty" - The email address is not formatted correctly (i.e. a correct email address should have this structure:
name@host.tld
). The message for this error should say "Looks like this is not an email"
- Any
- Solution URL: https://github.com/harnettd/intro-component-signup-form
- Live Site URL: https://harnettd.github.io/intro-component-signup-form/
- HTML
- CSS including Flexbox
- JavaScript
This is the first Frontend Mentor project that I worked on that asked for significant client-side form validation. To do so, I added an event listener to the submit button that checked for validity the values of each of the input elements:
form.addEventListener("submit", function (evt) {
if (firstName.value === "") {
addOnErr(firstName);
} else {
removeOnErr(firstName);
}
...
}
It's also the first time that I needed to use regular expressions in JavaScript. To check the validity of an email address, I used the following:
const isValidEmail = (email) => {
const emailRegexp = /^\w+(\.\w+)*@\w+(\.\w+)*$/g;
return email.value.match(emailRegexp) ? true : false;
}
Apparently, valid email addresses can be even more complicated than what's allowed for with the above regular expression, but this covers the vast majority of email addresses I've seen.
This is also the first time that I've made use of the focus and blur events, adding and removing CSS classes as needed:
input.addEventListener("focus", () => {
removeOnErr(input);
container.classList.add(focusClass);
});
input.addEventListener("blur", () => {
container.classList.remove(focusClass);
});
The key new ideas that I encountered on this project are HTML forms and client-side form validation. I'd like to dig deeper into how best to handle UX/UI considerations and provide useful visual cues and feedback to users.
- Regular Expressions - This is the page that I read in order to build a JavaScript regular expression that would match a (fairly) arbitrary email address.
- :focus - This is the resource I read to help me add appropriate styles to an input and its relevant relatives when the input gained focus.
As always, thanks to the people at Frontend Mentor for posting this challenge. Also, thanks to @skyv26 and @markuslewin for their helpful comments on my original submission.