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.
February 11, 2008 @ 1:10 pm
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.
February 14, 2008 @ 5:50 am
hey, nice explanation, its helped me allott.
More JavaScript Regular Expressions
February 20, 2008 @ 3:14 pm
[…] 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 […]
Validate Zip code using JavaScript Regular expression
February 20, 2008 @ 10:04 pm
[…] to validate zip code using regular expression in JavaScript. Previously we talked about validating email and Social Security number using JS […]
Validate U.S Phone Numbers using JavaScript Regular expression.
February 28, 2008 @ 10:44 pm
[…] JavaScript regular expression to validate U.S phone number. Previously we talked about validating email , Social Security number and zip code using JS […]
April 17, 2008 @ 12:52 am
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
April 21, 2008 @ 4:20 am
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
May 28, 2008 @ 1:23 am
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.
June 5, 2008 @ 1:58 pm
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).
June 19, 2008 @ 2:43 am
dfd
June 24, 2008 @ 2:46 pm
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!
July 26, 2008 @ 7:58 am
Hi,
Thank you very much for this article. It helps me for achieving my form. In addition, your explanation is clear and light.
August 13, 2008 @ 5:36 am
Good Job – really easy to understand explanation
August 19, 2008 @ 3:50 pm
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?
September 10, 2008 @ 5:53 am
Hi Zaheer,
Really clever solution, using regular expressions for validations.
I am studing at uni and we covered regular expressions on our
Unix system programing course. I see the usefulness of regular expressions now !
Keep posting more tips 😉
Cheers,
Armando
January 9, 2009 @ 4:44 pm
.@..ww
is a valid email address?
April 6, 2009 @ 2:05 am
thanks
April 15, 2009 @ 7:05 am
Not that I’m totally impressed, but this is more than I expected for when I found a link on SU telling that the info here is quite decent. Thanks.
May 15, 2009 @ 3:12 pm
Validate email address using regular expressions, nice piece of work. Very simple and clean. Thanks for the code. Helps me a lot.
— Jim
May 31, 2009 @ 9:47 am
This pattern isn’t a good for e-mail addresses. This pattern will filter out few of the WELL-FORMED e-mails and of MAL-FORMED as well.
June 17, 2009 @ 10:50 am
gonzo: that is quite an unfounded statement. could you please give examples of both. and if you are so smart, could you show solutions also? (there is already a comment showing it accepts faulty emails)
July 2, 2009 @ 2:37 pm
I too found that this regex has some flaws. An email like asdf@asdf would pass the filter. It doesn’t check for the .tld.
The guys at http://www.4guysfromrolla.com/webtech/052899-1.shtml have offered this, which ensures there’s a .tld :
^[\w-_.+]*[\w-_.]@([\w]+\.)+[\w]+[\w]$
I would encourage everyone to learn RegEx, it’s invaluable when you have to manipulate data, and who doesn’t these days. Zaheer’s lesson still teaches a lot.
@Mike VandeVelde
I believe apostrophes are illegal characters in emails.
July 23, 2009 @ 10:11 am
based on the above I think that:
/^[w.+-]+@[w.-]+.[a-z]{2,6}$/i
might be a better solution.
w to match alphanumeric characters and underscores
i on the end to be case insensitive
{2,6} rather than {2,4} to cope with .museum
this still doesn’t address .@..ww or similar though.
/^((w++*-*)+.?)+@((w++*-*)+.?)*[w-]+.[a-z]{2,6}$/i
Should deal with that problem though (I have not tested this fully but it works on those that I have tested which it should work on and catches those that are invalid which I tried)
(w++*-*) matches an more than one alphanumeric followed by zero or more +s and zero or more -s
(w++*-*)+ matches this happening at least once.
((w++*-*)+.?)+ matches this happening at least once with or without a . afterwards
((w++*-*)+.?)* is the same except that it matches 0 or more times.
[w-]+ matches at least 1 alphanumeric character or hyphen.
I hope this helps people.
August 12, 2009 @ 3:24 am
Daniel wrote:
/^((w++*-*)+.?)+@((w++*-*)+.?)*[w-]+.[a-z]{2,6}$/i
Nice one!
I tested it with something like:
x test
x test..@..com
x test..@example..com
x test..@example.example.com
x test.2@example.com.
v test.2@example.example.com
(x fails, v passes)
I used to have a script of my own that worked really nice (apart from 6-char TLD), but was about 45 lines of code…
Zaheer and Daniel: Thank you for this huge improvement 😉
August 12, 2009 @ 8:04 am
In the above test.@test.com is pass, also numerics / _ are allowed to start the email address, below regex works fine to some extent.
/^[a-zA-Z]([a-zA-Z0-9-_])*(.[a-zA-Z0-9-_])*@[a-zA-Z0-9](([a-zA-Z0-9-_.])*)+.[a-zA-Z]{2,4}$/
September 21, 2009 @ 10:08 pm
Thanks and congratulations to Zaheer for this article: It is very well written and it is easy to read. Thanks also to the rest of people who contributed to enhance this code.
I would like to share with you the following regex that I tried with all samples shown before (and some more, of course). I think it is worth a try:
^([A-Za-z0-9]{1,}([-_.&’][A-Za-z0-9]{1,}){0,}){1,}@(([A-Za-z0-9]{1,}[-]{0,1}).){1,}[A-Za-z]{2,6}$
I think this pretty well matches a great number of e-mail addresses.
Regards,
Francisco.
September 22, 2009 @ 5:57 am
Hai Latha,
I know its a very late reply for the question which you’ve asked. Anyway let it help the users who view this forum.
Your Question:
“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.”
Answer:
response.setHeader(“Cache-Control”,”no-cache,no-store”);
response.setDateHeader(“Expires”, 0);
response.setHeader(“Pragma”,”no-cache”);
just try to add the above code and check.
November 4, 2009 @ 6:39 am
That helped me a lot!!
THANK YOU!!
November 23, 2009 @ 7:57 am
good one..!
January 19, 2010 @ 11:11 am
Are mail-adresses with more than one “@” okay?
I was just wondering. Many of the regexp allow more than one “@”. Nevertheless some regexp allow combinations like test@test@example.org ….
March 23, 2010 @ 11:37 am
All exp failing at test:
test@example..org
Any solns for it ?
May 6, 2010 @ 3:46 am
Nice Post it will helps lot ,
How can we handle multiple email’s by using comma separated.
May 6, 2010 @ 3:50 am
any solution for (a@gmail.com,b@yahoo.co.in,c@gmail.com)
May 6, 2010 @ 4:01 am
Hi All,
am using this exp”” /^[a-zA-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}(?:(?:[,][A-Z0-9._%-]+@[A-Z0-9.-]+))?$/i “”
it’s handling this format very fine a@gmail.com,b@yahoo.co.in , while come to a@gmail.com,b@yahoo.co.in,c@gmail.com not working.
plz help me for this req
July 1, 2010 @ 7:18 am
This is just what I was looking for, thanks very much!
July 2, 2010 @ 1:04 am
Thanks. This RegEx pattern saved my day.
July 7, 2010 @ 3:24 am
Thanks to u Very Good Explanation of Regular Expressions
askkuber.com TEam
July 26, 2010 @ 10:39 pm
@Bhushna for you : /^[a-zA-Z0-9._%-]+@[A-Z0-9.-]+.[a-zA-Z]{2,4}(?:(?:[,][a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+))?$/i
Mailpress 5.0 Email Validation Bug | Made of Everything You're Not | Eric Lamb
August 10, 2010 @ 9:59 pm
[…] Mailpress would throw an error on the second email which was pissing of my client’s client and my client (sigh…). The fix is pretty stratightford and easy; just replace the regular expression in Mailpress with the working one I cribbed from Zaheer. […]
September 2, 2010 @ 9:56 am
Actually if you read the relevant RFCs, pretty much any character is technically valid before the final ‘@’ sign (at least in escaped or quoted sections) – the format of that part is left completely to the receiving mail server to interpret (“Abc@def”@example.com is valid, for example). The part after the ‘@’ sign is more restrictive, because only certain characters can be used in domain names.
You should certainly support apostrophes before the ‘@’, all but one of these regexes suggested will fail on valid email addresses like
jim.o’neill@example.com
September 29, 2010 @ 10:36 pm
Nice explanation.Now i really got the meaning of this regular expression.Untill now i was not known about the real meaning of this.This is nice and i thank you for the writter
October 5, 2010 @ 10:31 pm
thanx. nice explanation.
October 6, 2010 @ 8:26 am
Your expression does not validate Craigslist email addresses.
November 11, 2010 @ 12:45 am
Great, thanks for this post. It is useful for me.
November 19, 2010 @ 10:05 am
/^[a-z0-9,!#$%&’*+/=?^_`{|}~-]+(.[a-z0-9,!#$%&’*+/=?^_`{|}~-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*.([a-z]{2,})/i
not the best one, but works
November 27, 2010 @ 1:05 am
IT WORKS! THANKS THANKS THANKS.
December 5, 2010 @ 2:53 am
This helped me again as i forgot the regular expression and retrieved it from your site. Thanks a lot.
December 28, 2010 @ 4:19 pm
THANK YOU!!!
You have no idea how many websites I have gone through to find a simple explanation of this code so I may remake it without always copy-pasting just to get the job done.
You have helped a lot, thanks again.
December 29, 2010 @ 10:57 pm
That is nice .com it’s very useful for every one thansYou for giving this website
January 24, 2011 @ 11:15 am
HI FREND
January 26, 2011 @ 3:51 am
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
January 30, 2011 @ 3:52 pm
@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
February 1, 2011 @ 2:09 pm
It allows Testyahoo@yahoo..om , if we type two dots , it is validating as true.
February 18, 2011 @ 6:27 am
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.
March 16, 2011 @ 3:23 am
its not working n its allow String “r@#$” if i take only “[a-zA-Z0-9._-]+” as a patten;
March 20, 2011 @ 6:40 am
this one won’t work with a+b@g.com, right?
May 4, 2011 @ 12:49 pm
Use this regex instead
/^((([a-z]|d|[!#$%&’*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+(.([a-z]|d|[!#$%&’*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+)*)|((x22)((((x20|x09)*(x0dx0a))?(x20|x09)+)?(([x01-x08x0bx0cx0e-x1fx7f]|x21|[x23-x5b]|[x5d-x7e]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(\([x01-x09x0bx0cx0d-x7f]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))))*(((x20|x09)*(x0dx0a))?(x20|x09)+)?(x22)))@((([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).)+(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).?$/
May 24, 2011 @ 4:20 am
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 @ 8:19 am
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 (name@domain.extension) 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.
June 4, 2011 @ 12:09 pm
these are easy expressions to understand and aplliying…really i like it a very much.
June 9, 2011 @ 6:52 am
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
June 17, 2011 @ 9:15 am
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 abc@domain.co.uk, abc@domain.com.tw, abc@sub.domain.co.uk
July 13, 2011 @ 1:28 am
wewrer
July 19, 2011 @ 4:19 am
This email validation script is awesome, although I replaced it with Meserias’ modification. Thanks for the script!
August 3, 2011 @ 12:58 pm
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!
August 6, 2011 @ 1:00 pm
gr8….easy to understand and implement
August 13, 2011 @ 6:12 pm
Maddie’s regular expression doesn’t recognize a@b.com (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}$/
August 21, 2011 @ 6:33 pm
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 joe+list@domain.com (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.
August 26, 2011 @ 2:14 am
use this . from jquery.validate.js => no, its not, the longer the better. this just works
/^((([a-z]|d|[!#$%&’*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+(.([a-z]|d|[!#$%&’*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+)*)|((x22)((((x20|x09)*(x0dx0a))?(x20|x09)+)?(([x01-x08x0bx0cx0e-x1fx7f]|x21|[x23-x5b]|[x5d-x7e]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(\([x01-x09x0bx0cx0d-x7f]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))))*(((x20|x09)*(x0dx0a))?(x20|x09)+)?(x22)))@((([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).)+(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).?$/
September 3, 2011 @ 6:50 am
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. 🙂
September 8, 2011 @ 11:11 pm
not bad
September 21, 2011 @ 10:25 pm
thanx i got alot from this
October 3, 2011 @ 12:50 am
I used this expression and worked well.
/(^[a-z0-9!#$%&’*+/=?^_`{|}~-]+(.[a-z0-9!#$%&’*+/=?^_`{|}~-]+)*|”(?:[x01-x08x0bx0cx0e-x1fx21x23-x5bx5d-x7f]|\[x01-x09x0bx0cx0e-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
October 3, 2011 @ 8:14 am
greetings ..
i just want to know does this regex check dot at the beginning of email id because as per the expression we can have dot and digits at the start as well like for example:
.abc@yahoo.com
77def@fgt.com
Are these id’s valid?? please brief on that ..
Thankyou
October 28, 2011 @ 5:00 am
fantastic!!!!!!!. Thanks for this simple solution.
November 8, 2011 @ 6:14 am
how can we make the expression for a specific mail like gmail
November 17, 2011 @ 8:24 am
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!
November 23, 2011 @ 7:22 am
good 🙂
November 24, 2011 @ 2:59 am
what if someone inputs ‘joe@support.com.com’
or @.edu.edu
??
December 13, 2011 @ 5:29 am
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.
December 15, 2011 @ 3:19 am
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
}
December 16, 2011 @ 11:31 am
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!
December 31, 2011 @ 2:29 pm
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.
January 22, 2012 @ 1:57 am
ya zaheer paratha koon ha?????
January 24, 2012 @ 12:13 pm
The solution Tank gave seems to pass all my tests and it’s a one liner. Thanks Tank
February 2, 2012 @ 3:27 pm
Does Tank’s address a+anything@gmail.com? 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.
February 8, 2012 @ 9:09 am
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
February 13, 2012 @ 10:56 pm
thank you for helping to validate e-mail
February 14, 2012 @ 7:42 am
elseif($_POST[‘name’]==eregi(‘^[a-zA-Z ]+$’)) is correct
February 14, 2012 @ 7:44 am
error in eregi(‘^[a-zA-Z ]+$’))
February 27, 2012 @ 1:19 am
Hi
I want upload pdf book from any location in the PC to specific folder and save this path in mysql. With possibility of download this book.And when I want delete this PDf from the sit this PDf also delete from the folder and its path from the mysql .Please help me.
March 2, 2012 @ 1:17 am
Hi I need a code.I have ONE text box.When i am filling the text box,First letter should be alphanumeric and last 4 should be numbers.Give me replay ASAP.
March 3, 2012 @ 3:23 am
gud post buddy
March 3, 2012 @ 5:32 pm
Very helpful!
March 30, 2012 @ 6:30 am
try
/([w-.]+)@((?:[w]+.)+)([a-z]{2,4})/gi
April 3, 2012 @ 9:28 am
Asslam-u-Alaikum,
Very Informative and Helpful code. specially learners and beginners.
Wasalam
April 9, 2012 @ 12:41 am
Thanks everyone for your contributions.
I went with Tank’s solution and Kent’s modification. Does the trick for me!~
April 9, 2012 @ 1:24 pm
I’m using this and it’s working perfect:
mailRegex = /[a-z0-9!#$%&’*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&’*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])/;
This will help you validating wrong emails including: name@sample..com
Best Regards,
Mohamed
April 17, 2012 @ 8:38 am
Hey,
I’m glad I came across this thread and I hope I can get some help off you guys! I’m trying to work with an previously written JavaScript function to validate an email but it’s having problems if the user enters a space at the end of their email address (it happens, especially on mobile with auto-complete etc). How could I amend the following code to allow a space at the end?
function checkMail(emailInput) {
filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
if(!filter.test(document.lpform.email.value)) {
$(“#email”).addClass(“error”).focus();
$(“#email”).blur();
document.getElementById(‘dataStatus’).style.display=”block”;
return false; }
return true;
}
Thanks so much! I really appreciate it. =D
April 19, 2012 @ 4:56 am
Ok, I just added s and that worked…
filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Zs]{2,4}$/;
Thanks anyway.
May 1, 2012 @ 8:19 am
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/Vendor@ab.com
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 @ 9:32 am
very nice content it is very useful to me.
May 25, 2012 @ 1:03 am
fgsdf gsdfg