Validate email address using JavaScript

An email address is divided into two segments, namely the "personal-part" and the "domain-name," separated by the '@' symbol. The "personal-part" can be as long as 64 characters, while the "domain-name" can span up to 253 characters. This understanding is crucial when validating email addresses, as it aids in ensuring that these defined limits are upheld and that email formats adhere to these specifications.

  1. Uppercase: (A-Z) and lowercase (a-z) English letters.
  2. Digits: (0-9).
  3. Characters: ! # $ % & ' * + - / = ? ^ _ ` { | } ~
  4. Character: . ( period, dot or fullstop) provided that it is not the first or last character and it will not come one after the other.

The domain name [for example: com, org, net, in, us, info] part contains letters, digits, hyphens, and dots.

Example of valid email id

mysite@example.com

Relying solely on JavaScript for email validation is not a foolproof approach, as users can easily disable JavaScript in their browsers. This underscores the importance of performing server-side email validation as well. By incorporating both client-side (JavaScript) and server-side validation, you can ensure a more robust and comprehensive validation process that accounts for various scenarios and user behaviors.


validate email address using javascript/jquery

Regular Expression

A regular expression serves as an object that encapsulates a pattern of characters. This pattern is utilized to perform string matching or manipulation tasks, enabling developers to search for, extract, or replace specific sequences of characters within strings efficiently.

The following JavaScript shows how to validate an email address using Regular Expression .

function validateEmail(inText){ const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; var email=document.getElementById(inText).value; if(re.test(String(email).toLowerCase())) { alert("Email is valid : " + email); } else { alert("Email is not valid : " + email); } }
Full Source | JavaScript
<html> <head> <title>Email Validation</title> <script type="text/javascript"> function validateEmail(inText){ const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; var email=document.getElementById(inText).value; if(re.test(String(email).toLowerCase())) { alert("Email is valid : " + email); } else { alert("Email is not valid : " + email); } } </script> </head> <body> <h1>Validate Email address using JavaScript.</h1> <b>Enter Email Address: </b> <input type="text" id="textIn1"/><br><br> <input type="button" id="btnSum" value="Validate" onClick="validateEmail('textIn1')"/> </body> </html>

Conclusion

Validating an email address in JavaScript entails using a regular expression to verify if the input conforms to the standard email format. By employing this method, you can ensure that user-submitted email addresses are correctly structured before processing them further.