views:

7683

answers:

15

What's the best way to validate an email address in Javascript?

Though this solution may be simple, I'm sure this is one of those useful things that people will be Googling for and deserves its own entry on the site

+2  A: 

Just parse it with a regex, of which google will yield thouands, such as this

iAn
+8  A: 

javascript can match regex:

emailAddress.match( / some_regex /);

Here's an RFC22 regular expression for emails:

^((?>[a-zA-Z\d!#$%&'*+\-/=?^_{|}~]+\x20*|"((?=[\x01-\x7f])[^"\]|\[\x01-\x7f])"\x20)(?<))?((?!.)(?>.?[a-zA-Z\d!#$%&'+-/=?^_{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>)$

Ben Scheirman
+3  A: 

i usually use:

var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i ;
if (filter.test(email))
{
   //do stuff
}
else
{
   //validation error
}
John Boker
that filter is bum... doesn't allow for wildcards like [email protected]
AnApprentice
+25  A: 

Using Regular Expressions is probably the best way. Here's an example:

function validateEmail(email) 
{ 
 var 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,}))$/ 
 return email.match(re) 
}

But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.

sectrean
Thanks... sectrean Your answer is very nice
ungalnanban
Could someone explain what this will and won't match against? (Given some of the additional answers below.)It would be nice to know a bit more information in order to determine which 'solution' to use for a project.
Jon
+1: Only thing this won't let through are IPs instead of domains. It does accept domains starting or ending with a dash, and domains in excess of 64 chars in length, but other than this does a good enough job. Cheers.
Umber Ferrule
This regex eliminates valid, in-use emails. Do not use. Google for "RFC822" or "RFC2822" to get a proper regex.
Randal Schwartz
@Randall: Can you give an example of an email address that this won't let through?
Bryan Ross
+3  A: 

Answers to the questions Test/expand my email regex or How far should one take e-mail address validation? could easily be adapted for JavaScript

Sam Hasler
+1  A: 

There is a good example at http://techtamasha.com/?p=47

bmatthews68
+4  A: 

Wow, lots of complexity here, if all you want to do is just catch the most obvious syntax errors, I would do something like this:

\S+@\S+

It usually catches the most obvious errors that the user makes and assures that the form is mostly right, which is what javascript validation is all about.

Jaymon
+15  A: 

There's something you have to understand the second you decide to use a regular expression to validate emails: It's probably not a good idea. Once you have come to terms with that, there are many implementations out there that can get you halfway there, this article sums them up nicely.

In short, however, the only way to be absolutely, positively sure that what the user entered is in fact an email is to actually send an email and see what happens. Other than that it's all just guesses.

Paolo Bergantino
That link was a great read. Thanks!
Matt Ball
+2  A: 

Not bad, a somewhat stronger regex might be something like:

/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

I completely agree with Paolo that regex is not the optimum solution to this problem. But don't let perfect become the enemy of good. Regex validation is certainly better then no validation at all.

Chris Pebble
Dang! Beat me to it! :) +1 for having a great idea :)
Adam McKee
Will this work with + signs? For example [email protected]
jacobangel
Daniel LeCheminant
Daniel is right, this doesn't work with + signs, and doesn't work with new tlds like .travel or .museum.
Stef
+1  A: 

This was stolen from http://codesnippets.joyent.com/posts/show/1917

email = $('email');
filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (filter.test(email.value)) {
  // Yay! valid
  return true;
}
else
  {return false;}
Adam McKee
+1  A: 

/^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;

Vikram
+1  A: 

It's hard to get an email validator 100% correct. The only really way to get it correct would be to send a test email to the account. That said, there are a few basic checks that can help make sure that you're getting something reasonable.

Some things to improve:

Instead of new RegExp, just try writing the regexp out like this:

if (reg.test(/@/))

Second, check to make sure that a period comes after the @ sign, and make sure that there are characters between the @s and periods.

jacobangel
+5  A: 
voyager
+2  A: 

Here is a very good discussion about using regular expressions to validate email addresses; "Comparing E-mail Address Validating Regular Expressions"

Here is the current top expression, that is JavaScript compatible, for reference purposes:

/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i
spoon16
A: 

Apparently, that's it:

/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i

Taken from http://fightingforalostcause.net/misc/2006/compare-email-regex.php on Oct 1 '10.

But, of course, that's ignoring internationalization.

passcod
They forgot to escape the `@`.
Alan Moore
You don't need to.
passcod