I’m trying to add a text input to receive an email address. Then add an event handler using the DOM and addEventListener for the blur event. In the handler call the ValidateEmail event with the text entered. Use the event parameter to access the text box in the Target property. Use it’s Value to get the email address. If the return value of validate email is false change the background style of the text box to red. If it’s valid change it to “transparent”. I’m not sure if I’m close. Can anyone give me any pointers?
<!Doctype html>
<html>
<!--Header-->
<head>
<title>JavaScript Assignment 4- Jasmine Lusty</title>
<!--can contain javascript imports but page javascipt should be at the bottom of the body-->
</head>
<!--Body-->
<body>
<!---- Email Html--->
<h4>Email Validation!</h4>
<label for="inputEmail">Enter your email:</label>
<input type="email" id="input" name="email">
<!--javascript code-->
<script type="text/javascript">
var emailPattern = /[\w-\.]+@([\w-]+\.)+[\w-]{2,4}/;
input.onblur = function() {
if (!input.value.includes('/[\w-\.]+@([\w-]+\.)+[\w-]{2,4}/')) {
input.classList.add('invalid');
document.addEventListener("blur", function(){ document.style.backgroundColor = "transparent";});
} else{
document.addEventListener("blur", function(){ document.style.backgroundColor = "red";});
}
};
input.onfocus = function() {
if (this.classList.contains('invalid')) {
// remove the "error" indication, because the user wants to re-enter something
this.classList.remove('invalid');
error.innerHTML = "";
}
};
validateEmail(input)
</script>
<!--end of scripts-->
</body>
</html>