Using JavaScript how do I validate my email

Hi, right now I have it that an email is put into the function and it adds a paragraph saying if it is valid or not.
what I need to do is add a text box that someone would type their email into and then the background of the textbox would go transparent if the email is valid and the background would go red if it is not a valid email. I need to do this by using the addEventListener blur event. I’m not sure if I’m close?

<!Doctype html>
    <!--can contain javascript imports but page javascipt should be at the bottom of the body-->
<!---- Email Html--->
<h4>Email Validation!</h4>
	<label for="inputEmail">Enter your email:</label>
	<input type="email" id="emailInput" name="email">

<!--javascript code-->

<script type="text/javascript">

		function validateEmail(email, emailPattern){

	    emailPattern = (emailPattern instanceof RegExp) ?  emailPattern : /[\w-\.]+@([\w-]+\.)+[\w-]{2,4}/;
        var matchResults = emailPattern.test(email);
		var message  =  matchResults  ? 'Your Email is Valid' : 'Your Email is Invalid' ;
		var emailParagraph = document.createElement("p");
		return matchResults;
document.addEventListener("blur", function(validateEmail){ = "transparent";});

<!--end of scripts-->


Hi @jlusty, with this line you’re always setting the background colour to transparent when a blur event occurs; the actual validateEmail() function is only called once initially. When using an <input type="email"> though, you can actually use the intrinsic constraint validation API to check if the entered value is a valid email address:

<input type="email" id="email">
var email = document.getElementById('email')

email.addEventListener('blur', function (event) { =
    ? 'transparent'
    : 'red'
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.