so I am trying to validate that a valid email address was entered once the user finishes entering their email into the input element. If they didn't enter a valid email, it will change the elements border to red, if they did enter a correct email, it just leaves the border alone.
How can I do this WITHOUT submitting the form? I want it to happen either while the user is typing the email or once they unfocus the element.
This is what I have so far, but it is not working.
Thank you!
const emailInput = document.querySelector("#email");
const emailInputValue = emailInput.value;
emailInput.addEventListener("input", (e) => {
if( /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/.test(emailInputValue) != true) {
emailInput.style.border = "thin solid red"
} else {
emailInput.style.border = "thin solid #0000FF"
}
})
<div id="email_div">
<input type="email" name="email" id="email" placeholder="Email" maxlength="50">
<label for="email" id="email_text">This field is required in order to receive an email confirmation.</label>
</div>