Validate email address using JavaScript
An email address is a string separated into two parts by '@' symbol. A "personal-part" and a domain-name, that is personal-part@domain-name. The length of the personal-part may be up to 64 characters long and domain-name may be up to 253 characters . The personal-part contains the following ASCII characters.- Uppercase: (A-Z) and lowercase (a-z) English letters.
- Digits: (0-9).
- Characters: ! # $ % & ' * + - / = ? ^ _ ` { | } ~
- 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 Keep in mind that one should not rely only upon JavaScript email validation because JavaScript can easily be disabled.
Regular Expression
A regular expression is an object that describes a pattern of characters. 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>
Related Topics
- JavaScript Popup Boxes
- Opening a new window in JavaScript
- How to Create Drop-Down Lists in JavaScript
- How do I include a JavaScript file in another JavaScript file?
- Print the content of a Div using JavaScript/jQuery
- How to get the current URL using JavaScript ?
- How to Detect a Mobile Device with JavaScript/jQuery
- JavaScript Array Iteration
- How to Remove a Specific Item from an Array in JavaScript
- What is JavaScript closures?
- How To Remove a Property from a JavaScript Object
- How to get selected value from Dropdown list in JavaScript
- How do I get the current date in JavaScript?
- How to Open URL in New Tab | JavaScript
- How to delay/wait/Sleep in code execution | JavaScript
- How to round to at most 2 decimal places | JavaScript
- How to convert string to boolean | JavaScript
- How to check undefined in JavaScript?
- How To Copy to Clipboard | JavaScript
- How to encode a URL using JavaScript?
- How to force Input field to enter numbers only | JavaScript
- How to create multiline string in JavaScript
- How to Check for an Empty String in JavaScript?