How can an email address be validated in JavaScript?
|
Using regular expressions is probably the best way. Here's an example (live demo):
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. |
|||||||||||||||||||||
|
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. |
|||||||||||||||||||||
|
Just for completeness, here you have another RFC 2822 compliant regex
Emphasis mine |
|||||
|
I've slightly modified Jaymon's answer for people who want really simple validation in the form of: The regular expression:
Example JavaScript function:
|
|||||||||||||||||
|
Wow, there are lots of complexity here. If all you want to do is just catch the most obvious syntax errors, I would do something like this:
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. |
|||||||||||||||||||||
|
HTML5 itself has email validation. If your browser supports HTML5 then you can use the following code.
jsFiddle link |
|||||||||
|
Correct validation of email address in compliance with the RFCs is not something that can be achieved with a one-liner regular expression. An article with the best solution I've found in PHP is What is a valid email address?. Obviously, it has been ported to Java. I think the function is too complex to be ported and used in JavaScript. A good practice is to validate your data on the client, but double-check the validation on the server. With this in mind, you can simply check whether a string looks like a valid email address on the client and perform the strict check on the server. Here's the JavaScript function I use to check if a string looks like a valid mail address:
Explanation:
|
|||||||||
|
JavaScript can match a regular expression:
Here's an RFC22 regular expression for emails:
|
||||
|
In modern browsers you can build on top of @Sushil's answer with pure JavaScript and the DOM:
I've put together an example in the fiddle http://jsfiddle.net/boldewyn/2b6d5/. If you combine this with a Modernizr feature detection, it frees you from the regular expression massacre. |
|||||||||
|
Don't validate, just send a confirmation email instead. |
|||||||||||||
|
All email addresses contain an 'at' symbol. Test that necessary condition
Don't bother with anything more complicated. Even if you could perfectly determine whether an email is RFC-syntactically valid, that wouldn't tell you whether it belongs to the person who supplied it. That's what really matters. To test that, send a validation message. |
|||||||||
|
This is the correct RFC822 version.
|
|||||||||
|
This was stolen from http://codesnippets.joyent.com/posts/show/1917
|
|||||||||||||||||
|
Simply check out if the entered email address is valid or not using HTML.
There isn't any need to write a function for validation. |
|||||
|
You should not use regular expressions to validate an input string to check if it's an email. It's too complicated and would not cover all the cases. Now since you can only cover 90% of the cases, write something like:
You can refine it. For instance, 'aaa@' is valid. But overall you get the gist. And don't get carried away... A simple 90% solution is better than 100% solution that does not work. The world needs simpler code... |
|||||||||||||||||||||
|
Do this (case insensitive)
Why? It's based on RFC 2822, which is a standard ALL email addresses MUST adhere to. Here's an example of it being use in JavaScript
Note: Technically some emails can include quotes in the section before the @ symbol with escape characters inside the quotes (so you're email user can be obnoxious and contain stuff like @ and "... as long as it's written in quotes) NOBODY DOES THIS EVER. It's obsolete. But, it IS included in the true RFC 2822 standard, and omitted here. |
||||
|
This is how node-validator do it:
|
||||
|
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:
Second, check to make sure that a period comes after the @ sign, and make sure that there are characters between the @s and periods. |
||||
|
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:
|
|||||||||
|
Apparently, that's it:
Taken from http://fightingforalostcause.net/misc/2006/compare-email-regex.php on Oct 1 '10. But, of course, that's ignoring internationalization. |
||||
|
My knowledge of regular expressions is not that good. That's why I check the general syntax with a simple regular expression first and check more specific options with other functions afterwards. This may not be not the best technical solution, but this way I'm way more flexible and faster. The most common errors I've come across are spaces (especially at the beginning and end) and occasionally a double dot.
|
||||
|
Use this code inside your validator function:
Else you can use jQuery. Inside rules define:
|
|||||||||||||
|
Sectrean's solution works great, but it was failing my linter. So I added some escapes:
|
||||
|
|
||||
|
In contrast to squirtle, here is a complex solution, but it does a mighty fine job of validating emails properly:
Use like so:
|
|||||
|
|
||||
|
The regular expression provided by Microsoft within ASP.NET MVC is
Which I post here in case it's flawed - though it's always been perfect for my needs. |
||||
|
If you're using Closure you can use the built-in http://docs.closure-library.googlecode.com/git/class_goog_format_EmailAddress.html For example:
Note that by reading the source (linked above) you can see the comments state that IDN are not supported and that it only aims to cover most addresses:
|
||||
|
One of my coworker shared this regex with me. I like it a lot.
|
||||
|
|
||||
|
protected by Alan Moore Mar 2 '11 at 11:31
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?