Validate Email with JavaScript

What is JavaScript

Why Validate Email Address

Structure of Email Address

  1. Before @ :: It consists of the name of the email address. This part consists of letters (capital as well as small), numbers, and special characters.
  2. After @ :: It consists of a domain name so here you can use (capital as well as small), numbers, and special characters.
  3. After dot :: It defines the type of the domain like com, org, etc. In this part, only letters (capital as well as small), numbers, and dot(optional) can appear.

Validate Email with JavaScript

<!DOCTYPE html>
<html>
<body>
<h1>Validate Email with JavaScript</h1>
<input id="EmailAddress">
<button type="button" onclick="VALIDATE_EMAIL_ADDRESS()">Submit</button>
<h2 id="Result" style="color: Green;"></h2>
<script>
function VALIDATE_EMAIL_ADDRESS() {
var email;
email = document.getElementById("EmailAddress").value;
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if (reg.test(EmailAddress.value) == false)
{
document.getElementById("Result").style.color = "red";
document.getElementById("Result").innerHTML ="Invalid EMail Address "+ email;
return false;
}
else
{
document.getElementById("Result").style.color = "Green";
document.getElementById("Result").innerHTML ="Valid Email Address"+email;
}
return true;
}
</script>

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store