Created
September 21, 2015 01:57
-
-
Save andrewspear/9239b73859ea7fd35eb9 to your computer and use it in GitHub Desktop.
Reliable email validation with JS. The core regex could also be used in other programming languages.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function validateEmail(email) { | |
var re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | |
return re.test(email); | |
} | |
/* | |
This regex allows the following email formats: | |
1. [email protected] | |
2. [email protected] | |
3. [email protected] | |
4. [email protected] | |
9. #!$%&'*+-/=?^_`{}|[email protected] | |
6. "()[]:,;@\\\"!#$%&'*+-/=?^_`{}| ~.a"@example.org | |
7. " "@example.org (space between the quotes) | |
8. üñîçøðé@example.com (Unicode characters in local part) | |
9. üñîçøðé@üñîçøðé.com (Unicode characters in domain part) | |
10. Pelé@example.com (Latin) | |
11. δοκιμή@παράδειγμα.δοκιμή (Greek) | |
12. 我買@屋企.香港 (Chinese) | |
13. 甲斐@黒川.日本 (Japanese) | |
14. чебурашка@ящик-с-апельсинами.рф (Cyrillic) | |
It's clearly versatile and allows the all-important international characters, while still enforcing the basic [email protected] format. It will block spaces which are technically allowed by RFC, but they are so rare that I'm happy to do this. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment