MCPcopy
hub / github.com/validatorjs/validator.js / isEmail

Function isEmail

src/lib/isEmail.js:64–187  ·  view source on GitHub ↗
(str, options)

Source from the content-addressed store, hash-verified

62}
63
64export default function isEmail(str, options) {
65 assertString(str);
66 options = merge(options, default_email_options);
67
68 if (options.require_display_name || options.allow_display_name) {
69 const display_email = str.match(splitNameAddress);
70 if (display_email) {
71 let display_name = display_email[1];
72
73 // Remove display name and angle brackets to get email address
74 // Can be done in the regex but will introduce a ReDOS (See #1597 for more info)
75 str = str.replace(display_name, '').replace(/(^<|>$)/g, '');
76
77 // sometimes need to trim the last space to get the display name
78 // because there may be a space between display name and email address
79 // eg. myname <address@gmail.com>
80 // the display name is `myname` instead of `myname `, so need to trim the last space
81 if (display_name.endsWith(' ')) {
82 display_name = display_name.slice(0, -1);
83 }
84
85 if (!validateDisplayName(display_name)) {
86 return false;
87 }
88 } else if (options.require_display_name) {
89 return false;
90 }
91 }
92 if (!options.ignore_max_length && str.length > defaultMaxEmailLength) {
93 return false;
94 }
95
96 const parts = str.split('@');
97 const domain = parts.pop();
98 const lower_domain = domain.toLowerCase();
99
100 if (options.host_blacklist.length > 0 && checkHost(lower_domain, options.host_blacklist)) {
101 return false;
102 }
103
104 if (options.host_whitelist.length > 0 && !checkHost(lower_domain, options.host_whitelist)) {
105 return false;
106 }
107
108 let user = parts.join('@');
109
110 if (options.domain_specific_validation && (lower_domain === 'gmail.com' || lower_domain === 'googlemail.com')) {
111 /*
112 Previously we removed dots for gmail addresses before validating.
113 This was removed because it allows `multiple..dots@gmail.com`
114 to be reported as valid, but it is not.
115 Gmail only normalizes single dots, removing them from here is pointless,
116 should be done in normalizeEmail
117 */
118 user = user.toLowerCase();
119
120 // Removing sub-address from username before gmail validation
121 const username = user.split('+')[0];

Callers 1

isMailtoURIFunction · 0.85

Calls 7

assertStringFunction · 0.85
mergeFunction · 0.85
validateDisplayNameFunction · 0.85
checkHostFunction · 0.85
isByteLengthFunction · 0.85
isFQDNFunction · 0.85
isIPFunction · 0.85

Tested by

no test coverage detected