Design comparison
Solution retrospective
I had trouble creating the custom error message. I figured out that if I set the type to 'email' in the html form input it would handle the input validation for an email. However, it took some time to figure out how to combine this with JavaScript. By using the "invalid" argument in an event listener for the form input allowed me to do this.
Community feedback
- @MikDra1Posted 3 months ago
Nice one
If you are cerious there is also a CSS property that we can use to check wether the input is in valid or invalid state and here is how it works
/* Style for invalid input fields */ input:invalid { border: 2px solid red; /* Red border for invalid input */ background-color: #ffe6e6; /* Light red background */ color: #a94442; /* Darker red text */ } input:invalid:focus { outline: none; box-shadow: 0 0 5px red; /* Red shadow when focused */ } textarea:invalid, select:invalid { border: 2px solid red; background-color: #ffe6e6; color: #a94442; }
Explanation:
-
input:invalid: This targets any <input> element that is invalid according to its validation rules (e.g., type="email" without a valid email format).
-
background-color and border: Provide visual feedback with a red border and a light red background.
-
input:invalid:focus: Adds a red shadow when the invalid input is focused, making it stand out.
Marked as helpful1 -
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord