isEmail.js
4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isEmail;
var _assertString = _interopRequireDefault(require("./util/assertString"));
var _merge = _interopRequireDefault(require("./util/merge"));
var _isByteLength = _interopRequireDefault(require("./isByteLength"));
var _isFQDN = _interopRequireDefault(require("./isFQDN"));
var _isIP = _interopRequireDefault(require("./isIP"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var default_email_options = {
allow_display_name: false,
require_display_name: false,
allow_utf8_local_part: true,
require_tld: true
};
/* eslint-disable max-len */
/* eslint-disable no-control-regex */
var displayName = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\,\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\s]*<(.+)>$/i;
var emailUserPart = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i;
var gmailUserPart = /^[a-z\d]+$/;
var quotedEmailUser = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i;
var emailUserUtf8Part = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i;
var quotedEmailUserUtf8 = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i;
/* eslint-enable max-len */
/* eslint-enable no-control-regex */
function isEmail(str, options) {
(0, _assertString.default)(str);
options = (0, _merge.default)(options, default_email_options);
if (options.require_display_name || options.allow_display_name) {
var display_email = str.match(displayName);
if (display_email) {
str = display_email[1];
} else if (options.require_display_name) {
return false;
}
}
var parts = str.split('@');
var domain = parts.pop();
var user = parts.join('@');
var lower_domain = domain.toLowerCase();
if (options.domain_specific_validation && (lower_domain === 'gmail.com' || lower_domain === 'googlemail.com')) {
/*
Previously we removed dots for gmail addresses before validating.
This was removed because it allows `multiple..dots@gmail.com`
to be reported as valid, but it is not.
Gmail only normalizes single dots, removing them from here is pointless,
should be done in normalizeEmail
*/
user = user.toLowerCase(); // Removing sub-address from username before gmail validation
var username = user.split('+')[0]; // Dots are not included in gmail length restriction
if (!(0, _isByteLength.default)(username.replace('.', ''), {
min: 6,
max: 30
})) {
return false;
}
var _user_parts = username.split('.');
for (var i = 0; i < _user_parts.length; i++) {
if (!gmailUserPart.test(_user_parts[i])) {
return false;
}
}
}
if (!(0, _isByteLength.default)(user, {
max: 64
}) || !(0, _isByteLength.default)(domain, {
max: 254
})) {
return false;
}
if (!(0, _isFQDN.default)(domain, {
require_tld: options.require_tld
})) {
if (!options.allow_ip_domain) {
return false;
}
if (!(0, _isIP.default)(domain)) {
if (!domain.startsWith('[') || !domain.endsWith(']')) {
return false;
}
var noBracketdomain = domain.substr(1, domain.length - 2);
if (noBracketdomain.length === 0 || !(0, _isIP.default)(noBracketdomain)) {
return false;
}
}
}
if (user[0] === '"') {
user = user.slice(1, user.length - 1);
return options.allow_utf8_local_part ? quotedEmailUserUtf8.test(user) : quotedEmailUser.test(user);
}
var pattern = options.allow_utf8_local_part ? emailUserUtf8Part : emailUserPart;
var user_parts = user.split('.');
for (var _i = 0; _i < user_parts.length; _i++) {
if (!pattern.test(user_parts[_i])) {
return false;
}
}
return true;
}
module.exports = exports.default;
module.exports.default = exports.default;