how to check is a input email is valid with js code example

Example 1: email regex javascript

/* Answer to: "email regex javascript" */

ValidateEmail("[email protected]"); // Must be a string

function ValidateEmail(email) {
	var emailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(String(email).toLowerCase());
	if (email.match(emailformat)) {
    	alert("Nice Email!")
      	return true;
    };
    alert("That's not an email?!")
    return (false);
};

Example 2: email validation js

/// if you want  create your regular expression then you can  follow this site 
/////https://regex101.com///////
<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
<form method="post" action="">
	Email:<input type="text"  id="email" onkeyup="validation()">
	</form>
</body>
<script type="text/javascript">
	function validation(){
	var email=document.getElementById("email").value;///get id with value 
	var emailpattern=/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;////Regular expression
	if(emailpattern.test(email))
	{
		document.getElementById("email").style.backgroundColor='yellow';
    }
    else
    {
    	document.getElementById("email").style.backgroundColor='red'; }
	}

</script>
</html>