Validate email address using JavaScript regular expression
Feb 07
Last month I wrote about regular expressions in Java, today I’ll show you how to use regular expression in JavaScript to validate email address.
Here is the code to validate email address in JavaScript using regular expression.
function validateEmail(elementValue){
var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return emailPattern.test(elementValue);
}
Explanation:
The argument to this method is the email address you want to validate.
In the method body we define a variable (‘emailPattern’) and assign a regular expression to it.
/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/
To understand the regular expression we will divide it into smaller components:
/^[a-zA-Z0-9._-]+: Means that the email address must begin with alpha-numeric characters (both lowercase and uppercase characters are allowed). It may have periods,underscores and hyphens.
@: There must be a ‘@’ symbol after initial characters.
[a-zA-Z0-9.-]+: After the ‘@’ sign there must be some alpha-numeric characters. It can also contain period (‘.’) and and hyphens(‘-’).
\.: After the second group of characters there must be a period (‘.’). This is to separate domain and subdomain names.
[a-zA-Z]{2,4}$/: Finally, the email address must end with two to four alphabets. Having a-z and A-Z means that both lowercase and uppercase letters are allowed.
{2,4} indicates the minimum and maximum number of characters. This will allow domain names with 2, 3 and 4 characters e.g.; us, tx, org, com, net, wxyz).
On the final line we call test method for our regular expression and pass the email address as input. If the input email address satisfies our regular expression, ‘test’ will return true otherwise it will return false. We return this value to the calling method.
You can call this method whenever you want to validate email address.
Related posts:
- Ultimate Java Regular Expression to validate any email address.
- Validate Zip code using JavaScript Regular expression
- Validate U.S Phone Numbers using JavaScript Regular expression.
- How to validate Social Security Number (SSN) using JavaScript Regular Expressions
- How to validate email, SSN, phone number in Java using Regular expressions.



Jan 24, 2011 @ 11:15:09
HI FREND
Jan 26, 2011 @ 03:51:57
I noticed that you do not allow for domains such as co.uk co.il and I am sure there are others. you might want to revise…….. but thank anyway
Jan 30, 2011 @ 15:52:11
@Pnina, the code will work fine on co.uk domains as the regex check after the “@” sign allows multiple periods (“.”) to be there (this allows for subdomains to be allowed as well) so as long as the last set of digits after the period are 2-4 digits long the validation will work
Feb 01, 2011 @ 14:09:23
It allows Testyahoo@yahoo..om , if we type two dots , it is validating as true.
Feb 18, 2011 @ 06:27:51
I didn’t know that you can validate the email address using JavaScript regular expression. I suppose it’s the best option if you need to validate in a easiest way this task.
Mar 16, 2011 @ 03:23:01
its not working n its allow String “r@#$” if i take only “[a-zA-Z0-9._-]+” as a patten;
Mar 20, 2011 @ 06:40:16
this one won’t work with [email protected], right?
May 04, 2011 @ 12:49:59
Use this regex instead
/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/
May 24, 2011 @ 04:20:28
This just returns the value of the email you pass it? It dosen’t result in a true/false test? How is this of any use?
May 24, 2011 @ 08:19:53
ATENTION: this regex has a bug. The right syntax is:
/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+[\.]{1}[a-zA-Z]{2,4}$/
Without this modification a string like name@yahoo is a valid emalil address.
for Confused: this function return true if the user type a valid email address ([email protected]) and false if not.
You can call the function in this way:
var valid_email=validateEmail(document.getElementById['field_id'].value);
if(valid_email){ [your code] }else{ [your code] }
This validation is for you users, but you must do another validation on server side when you save your data.
Jun 04, 2011 @ 12:09:29
these are easy expressions to understand and aplliying…really i like it a very much.
Jun 09, 2011 @ 06:52:01
function val()
{
var email = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+[\.]{1}[a-zA-Z]{2,4}$/;
}
if(document.f.t3.value.search(email)==-1)
{
alert (“plz enter tha email”);
return false;
}
Email
Jun 17, 2011 @ 09:15:55
I modified this a bit to allow sub domains, multiple tld’s and removed validating multiple .’s (user@domain..com)
(^[a-zA-Z0-9._-]+@[a-zA-Z0-9]+([.-]?[a-zA-Z0-9]+)?([\.]{1}[a-zA-Z]{2,4}){1,4}$)
will validate [email protected], [email protected], [email protected]
Jul 13, 2011 @ 01:28:48
wewrer
Jul 19, 2011 @ 04:19:52
This email validation script is awesome, although I replaced it with Meserias’ modification. Thanks for the script!
Aug 03, 2011 @ 12:58:21
Actually Maserias’ modification works really well except that it accepts abc@.xyz.com. “@.” shouldn’t be together. I made a minor modification to it:
/^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9.-]+[\.]{1}[a-zA-Z]{2,4}$/
So this checks to see that the domain starts with alphanumeric characters. The credit however goes to Meserias!
Aug 06, 2011 @ 13:00:29
gr8….easy to understand and implement
Aug 13, 2011 @ 18:12:58
Maddie’s regular expression doesn’t recognize [email protected] (exactly one character between ‘@’ and ‘.’. This modification corrects this problem:
/^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9.-]*[\.]{1}[a-zA-Z]{2,4}$/
Aug 21, 2011 @ 18:33:48
It’s a pet peeve of mine that people publish regular expressions for email addresses when they simply don’t know the standard. Every single regular expression on this page, article and comments alike, fails to validate email addresses of the form [email protected] (though to be honest I didn’t check all the older comments). People grab these expressions and throw them into their code, thinking that will make them fit some sort of standard, when they don’t.
The inability to add the +suffix to an email address undermines the usefulness of the feature even where it is accepted. PLEASE add proper support for the standard and spread the word.
There’s a good article at http://www.regular-expressions.info/email.html that discusses tradeoffs of various regular expressions for validating email addresses.
On this form I’m using a + sign in my email address.
Aug 26, 2011 @ 02:14:57
use this . from jquery.validate.js => no, its not, the longer the better. this just works
/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/
Sep 03, 2011 @ 06:50:29
Thanks this is a simple and elegant solution to the problem.
Also thanks to Tank. I found his modification to the expression to be the best for my needs.
Sep 08, 2011 @ 23:11:59
not bad
Sep 21, 2011 @ 22:25:32
thanx i got alot from this
Oct 03, 2011 @ 00:50:16
I used this expression and worked well.
/(^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|”(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*”)(\.)?@(([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}|(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))$/i
As a Java String
“(^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\”(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\”)(\\.)?@(([a-z0-9]([a-z0-9-]*[a-z0-9])?\\.)+[a-z]{2,}|(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))$”
Of-course the length part of the email is not considered in the regular expression.
This will allow ! # $ % & ‘ * + – / = ? ^ _ ` { | } ~ in the local part and the local part can end with dot (.)
domain part can be IP or domain
Oct 28, 2011 @ 05:00:20
fantastic!!!!!!!. Thanks for this simple solution.
Nov 08, 2011 @ 06:14:08
how can we make the expression for a specific mail like gmail
Nov 17, 2011 @ 08:24:40
To end on “@gmail.com” just use this as final part:
/\@gamil\.com$/
If want to check anything before @ add checks at begining.
This one will only allow leters, numbers and ‘_’ and ‘-’ as left part of @ and must be on gmail.com
/^[0-9A-Za-z\_\-]+\@gamil\.com$/
An this other one is also to accept dot on user part, but not at start neither near the @:
/^[0-9A-Za-z\_\-]([\.][0-9A-Za-z\_\-]+)*\@gamil\.com$/
Hope helps!
Nov 23, 2011 @ 07:22:19
good
Nov 24, 2011 @ 02:59:57
what if someone inputs ‘[email protected]’
or @.edu.edu
??
Dec 13, 2011 @ 05:29:48
This RegEx allows multiple email addresses if it is separated by a “;”
In a textbox if i need to validate a single email address and if two addresses are entered then this RegEx wont work.
Dec 15, 2011 @ 03:19:35
Hi
I am VERY new too Javascript and need to validate a form for a tafe assignment. So far I have added code to validate required text fields and a drop down menu and have all the error messages come up in the one alert box. Now I am having trouble figuring out how to add the code to validate an email and have the error message appear in the same alert box as they are other fields are being validated. I hope I am making sense!!
If anyone is able to help this is my code! Please ignore all the comments! I have to add them as parrt of the assignment
function checkforblank() {
var errormessage = “”;
if (document.getElementById(‘selectmenu’).value == “”) {
/* the getElementById is determined by the id given in the below html.
For example: */
errormessage += “Please specify your title \n \n”;
document.getElementById (‘selectmenu’).style.borderColor = “red”;
/* .style.borderColor highlights the borders of the text fields in red when there is an error */
}
if (document.getElementById(‘fname’).value == “”) {
errormessage += “Please enter your first name \n \n”;
/* error message if first name field is not filled out */
document.getElementById (‘fname’).style.borderColor = “red”;
}
if (document.getElementById(‘lname’).value == “”) {
errormessage += “Please enter your last name \n \n”;
/* error message if last name field is not filled out */
document.getElementById (‘lname’).style.borderColor = “red”;
}
if (document.getElementById(‘enquiry’).value == “”) {
errormessage += “Please submit an enquiry \n \n”;
/* error message if viewer does not enter an enquiry */
document.getElementById (‘enquiry’).style.borderColor = “red”;
}
if (errormessage != “”) {
alert (errormessage);
/* alert (errormessage) displays the specified error messages that were created in the
above if statements */
return false;
}
/* return false stops the form from being submitted to the server */
// End of function
}
Dec 16, 2011 @ 11:31:37
This regex has so many flaws I have to heavily recommend everyone ignore it. Worse than the numerous false positives others have already pointed out, it also regexs common perfectly valid email addresses. For example, the + character is very valid and not super uncommon in the left hand side of an email address.
Buyer beware, you get what you pay for when you use this regex!
Dec 31, 2011 @ 14:29:26
here’s the regex modified to accept + signs after the first character of the address:
/^[a-z0-9._\-][a-z0-9._\-\+]*@[a-z0-9][a-z0-9.\-]*[\.]{1}[a-z]{2,4}$/i
I also shortened it by adding i modifier at end, so don’t need to specify A-Z for uppercase letters. The regex still has false positives, no doubt, but no false negatives for + signs.
Jan 22, 2012 @ 01:57:56
ya zaheer paratha koon ha?????
Jan 24, 2012 @ 12:13:22
The solution Tank gave seems to pass all my tests and it’s a one liner. Thanks Tank
Feb 02, 2012 @ 15:27:48
Does Tank’s address [email protected]? It did not seem too. Jerry mentioned this.
This is a feature at gmail, add a “+(anything you pick)” before the @ on your regular gmail address, and the email will still be delivered to you, but you can send it to special filters.
Modifying Tanks to this
/^[a-zA-Z0-9._\+-]+@[a-zA-Z0-9]+([.-]?[a-zA-Z0-9]+)?([\.]{1}[a-zA-Z]{2,4}){1,4}$/;
Seemed to work.
Feb 08, 2012 @ 09:09:08
Hi,
can some one please explain the meaning of below regular expression :
“^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$”;
thanks in advance