Hi everyone! I’m working on a login page and I want to make the input fields behave like those on popular email services. Here’s what I’m trying to do:
When the user focuses on an input field, I want the border to turn blue.
If the user enters invalid data, the border should change to red.
I’m not sure how to achieve this effect. Can anyone share some CSS or JavaScript tips to make this work? I’ve tried a few things but can’t seem to get it right. Any help would be awesome!
For validation, you’ll need some JavaScript. Add an event listener to your input fields that checks the input value on blur (when the user clicks away). If it’s invalid, add an ‘error’ class to the input. Then in your CSS:
input.error {
border-color: red;
}
This approach gives you flexibility to define what ‘invalid’ means for each field. You could also use HTML5 form validation attributes like ‘required’ or ‘pattern’, and style the :invalid pseudo-class instead.
Remember to provide visual feedback beyond just color changes for accessibility. Maybe add an icon or error message for invalid inputs.