UNPKG

1.05 kBJavaScriptView Raw
1/* eslint no-useless-escape: "error" */
2'use strict';
3
4const tester = /^[-!#$%&'*+\/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-?\.?[a-zA-Z0-9])*\.[a-zA-Z](-?[a-zA-Z0-9])+$/; // eslint-disable-line no-useless-escape
5// Thanks to:
6// http://fightingforalostcause.net/misc/2006/compare-email-regex.php
7// http://thedailywtf.com/Articles/Validating_Email_Addresses.aspx
8// http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses/201378#201378
9exports.validate = function (email) {
10 if (!email) {
11 return false;
12 }
13
14 if (email.length > 254) {
15 return false;
16 }
17
18 const valid = tester.test(email);
19 if (!valid) {
20 return false;
21 }
22
23 // Further checking of some things regex can't handle
24 const parts = email.split('@');
25 if (parts[0].length > 64) {
26 return false;
27 }
28
29 const domainParts = parts[1].split('.');
30 if (domainParts.some(function (part) { // eslint-disable-line prefer-arrow-callback
31 return part.length > 63;
32 })) {
33 return false;
34 }
35
36 return true;
37};