Validate email address using JavaScript regular expression
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.
Email format: The regular expression for email is
/^[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.
If you enjoyed this post, make sure you subscribe to my RSS feed!
Related Posts:








Bret:
Hey, nice useful piece of code. Always nice to find something that takes a fairly common task, such as email validation, and breaks down the logic and provides a simple piece of code in the end.
Posted on February 11th, 2008 at 1:10 pm
zparacha:
Bret, I am glad you liked this post. Thanks for visiting.
Posted on February 12th, 2008 at 11:02 pm
Mastan Jhoni:
hey, nice explanation, its helped me allott.
Posted on February 14th, 2008 at 5:50 am
More JavaScript Regular Expressions:
[...] I wrote the JS regular expression article last week I had no idea that it will attract so many visitors. Not only did so many people read [...]
Posted on February 20th, 2008 at 3:14 pm
Validate Zip code using JavaScript Regular expression:
[...] to validate zip code using regular expression in JavaScript. Previously we talked about validating email and Social Security number using JS [...]
Posted on February 20th, 2008 at 10:04 pm
Validate U.S Phone Numbers using JavaScript Regular expression.:
[...] JavaScript regular expression to validate U.S phone number. Previously we talked about validating email , Social Security number and zip code using JS [...]
Posted on February 28th, 2008 at 10:44 pm
Latha p:
Hi , email validation code present in this site is very nice and it is very useful to validate email address using regular expressions. My sincere thanks to the programmer who posted this. This help me alot.
Thanks & Regards,
Latha
Posted on April 17th, 2008 at 12:52 am
Zaheer:
Latha, I am glad that you found this post helpful. I appreciate your comments.
Posted on April 17th, 2008 at 6:52 am
Latha:
Hi can anyone help me how to invalidate the session , when i am clicking LOGOUT button. I use session.invalidate() method , the page has been expired. But when click back button on browser it goes to back page. How to avoid this. I request you any body can solve this problem.
yours
Latha
Posted on April 21st, 2008 at 4:20 am
Swaroop:
Hi,
Very nice one which helped me a lot.
I tried the below code for checking alphabets, hyphen and underscore. However its not working.
Could any of you help me on this.
function validateChars(elementValue)
{
var charsPattern = /[a-zA-Z0-9_-]*/;
return charsPattern.test(elementValue);
}
Thanks.
Posted on May 28th, 2008 at 1:23 am
Blaise Kal:
Thanks for this. Saved me some work.
There is a small error in the regexp: top-level domains could be longer than 4 characters (like .travel and .museum).
Posted on June 5th, 2008 at 1:58 pm
J.Rajesh Joseph:
dfd
Posted on June 19th, 2008 at 2:43 am
Mike VandeVelde:
Don’t you need to escape the period? ie:
[a-zA-Z0-9._-]
should be:
[a-zA-Z0-9\._-]
Otherwise, isn’t a period a single character wildcard?
And can’t a-zA-Z0-9 be replaced with \\w?
ps Should also add apostrophe ‘ for Irish names, that’s the reason I was hunting this stuff down!
Posted on June 24th, 2008 at 2:46 pm
Josue:
Hi,
Thank you very much for this article. It helps me for achieving my form. In addition, your explanation is clear and light.
Posted on July 26th, 2008 at 7:58 am
Zaheer:
Josue, I am glad that you found this post useful.
Posted on July 26th, 2008 at 11:36 pm
Dario:
Good Job - really easy to understand explanation
Posted on August 13th, 2008 at 5:36 am
ray sweeten:
Hey Mike,
[a-zA-Z0-9_] can be replaced with \w.
also, because the . is included in the second character class(after @), this would be considered a valid email:
a@a…….com
not sure what the solution to that is. .
\.{1} does limit the repetition of . to one, but since the previous characters have already been passed, the limiter becomes irrelevant.
anyone?
Posted on August 19th, 2008 at 3:50 pm