To validate email address in JavaScript, check the condition for “@” and “.” i.e. for [email protected] . Let’s try the following code to validate email
Example
Live Demo
<!DOCTYPE html>
<html>
<body>
<script>
var emailID;
function validateEmail(emailID) {
atpos = emailID.indexOf("@");
dotpos = emailID.lastIndexOf(".");
if (atpos < 1 || ( dotpos - atpos < 2 )) {
document.write("Please enter correct email ID")
document.myForm.EMail.focus() ;
return false;
}
return( true );
}
document.write(validateEmail("[email protected]"));
</script>
</body>
</html>