Skip to content

Instantly share code, notes, and snippets.

@andrewspear
Created September 21, 2015 01:57
Show Gist options
  • Save andrewspear/9239b73859ea7fd35eb9 to your computer and use it in GitHub Desktop.
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.
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