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.






Apr 19, 2012 @ 04:56:15
Ok, I just added \s and that worked…
filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z\s]{2,4}$/;
Thanks anyway.
May 01, 2012 @ 08:19:14
Hello All,
I am Glad that i have landed to this email. I have a new requirement for validating email for a new format, can anyone tell me how to by pass.
Contact350test/[email protected]
I tried removing / from the below exemplist, But the code is throwing JS error.
var exemplist =/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]*@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]*\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
Thanks in advance.
May 14, 2012 @ 09:32:13
very nice content it is very useful to me.
May 25, 2012 @ 01:03:36
fgsdf gsdfg
May 25, 2012 @ 01:05:16
cvcvbcvb cv b
Jun 19, 2012 @ 23:55:54
I neead to receio my email
Jun 19, 2012 @ 23:57:55
I
Med.
Jun 19, 2012 @ 23:59:23
Thanks
Jun 20, 2012 @ 00:00:06
I breaste
Jul 04, 2012 @ 01:24:41
Valid email-id for all different kinds of email-ids(e.g. [email protected] OR [email protected] OR [email protected].
Regular expression for Email-Id– /^([a-zA-Z0-9_\-])+\@(([a-zA-Z0-9\-])+)+(\.[a-zA-Z]{2,6})(\.[a-zA-Z]{2})?$/i
Jul 16, 2012 @ 01:32:08
@John I hope i’m not too late to answer you. Try this
/^(([a-zA-Z0-9._-])||([a-zA-Z0-9._-]+\/))+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/
to bypass this Contact350test/[email protected]
Jul 17, 2012 @ 05:19:22
very nice..easy to learn or do…
Sep 18, 2012 @ 14:11:37
Pity that this does not take account with emails like [email protected]
Oct 11, 2012 @ 06:05:40
Great dude
we were looking for the regular expressions explanation since long.
Thanx a ton
Apr 22, 2013 @ 01:47:50
this is gud
Apr 29, 2013 @ 07:06:25
Thank you for your regexp, it really works!