Skip to content

Commit

Permalink
Add gmail validation to isEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
tux-tn committed May 15, 2018
1 parent 69bdc8b commit 58cd58c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
12 changes: 12 additions & 0 deletions lib/isEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var default_email_options = {
/* 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](\.?[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;
Expand Down Expand Up @@ -68,6 +69,17 @@ function isEmail(str, options) {
should be done in normalizeEmail
*/
user = user.toLowerCase();

// Removing sub-address from username before gmail validation
var username = user.split('+')[0];

if (!(0, _isByteLength2.default)(username, { min: 6, max: 30 })) {
return false;
}

if (!gmailUserPart.test(username)) {
return false;
}
}

if (!(0, _isByteLength2.default)(user, { max: 64 }) || !(0, _isByteLength2.default)(domain, { max: 254 })) {
Expand Down
12 changes: 12 additions & 0 deletions src/lib/isEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const default_email_options = {
/* eslint-disable no-control-regex */
const displayName = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\,\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\s]*<(.+)>$/i;
const emailUserPart = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i;
const gmailUserPart = /^[a-z\d](\.?[a-z\d])+$/;
const quotedEmailUser = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i;
const emailUserUtf8Part = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i;
const 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;
Expand Down Expand Up @@ -49,6 +50,17 @@ export default function isEmail(str, options) {
should be done in normalizeEmail
*/
user = user.toLowerCase();

// Removing sub-address from username before gmail validation
const username = user.split('+')[0];

if (!isByteLength(username, { min: 6, max: 30 })) {
return false;
}

if (!gmailUserPart.test(username)) {
return false;
}
}

if (!isByteLength(user, { max: 64 }) ||
Expand Down
40 changes: 22 additions & 18 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ describe('Validators', function () {
'hans.m端[email protected]',
'hans@m端ller.com',
'test|123@m端ller.com',
'test[email protected]',
'some.name.midd.leNa.me.[email protected]',
'test123[email protected]',
'[email protected]',
'"foobar"@example.com',
'" foo m端ller "@example.com',
'"foo\\@bar"@example.com',
Expand All @@ -73,6 +73,7 @@ describe('Validators', function () {
`${repeat('a', 64)}@${repeat('a', 251)}.com`,
`${repeat('a', 65)}@${repeat('a', 250)}.com`,
`${repeat('a', 64)}@${repeat('a', 64)}.com`,
`${repeat('a', 31)}@gmail.com`,
'[email protected] m',
'[email protected] m',
'[email protected] m',
Expand All @@ -87,6 +88,8 @@ describe('Validators', function () {
'[email protected] m',
'[email protected] m',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
],
Expand All @@ -104,8 +107,8 @@ describe('Validators', function () {
'[email protected]',
'hans@m端ller.com',
'test|123@m端ller.com',
'test[email protected]',
'some.name.midd.leNa.me.[email protected]',
'test123[email protected]',
'[email protected]',
'"foobar"@example.com',
'"foo\\@bar"@example.com',
'" foo bar "@example.com',
Expand Down Expand Up @@ -136,21 +139,21 @@ describe('Validators', function () {
'hans.m端[email protected]',
'hans@m端ller.com',
'test|123@m端ller.com',
'test[email protected]',
'some.name.midd.leNa.me.[email protected]',
'test123[email protected]',
'[email protected]',
'Some Name <[email protected]>',
'Some Name <[email protected]>',
'Some Name <[email protected]>',
'Some Name <[email protected]>',
'Some Name <hans.m端[email protected]>',
'Some Name <hans@m端ller.com>',
'Some Name <test|123@m端ller.com>',
'Some Name <test[email protected]>',
'Some Name <test123[email protected]>',
'\'Foo Bar, Esq\'<[email protected]>',
'Some Name <some.name.midd.leNa.me.[email protected]>',
'Some Middle Name <some.name.midd.leNa.me.[email protected]>',
'Name <some.name.midd.leNa.me.[email protected]>',
'Name<some.name.midd.leNa.me.[email protected]>',
'Some Name <[email protected]>',
'Some Middle Name <[email protected]>',
'Name <[email protected]>',
'Name<[email protected]>',
],
invalid: [
'invalidemail@',
Expand All @@ -168,6 +171,7 @@ describe('Validators', function () {
'Some Name < [email protected] >',
'Name [email protected]',
'Some Name <[email protected]>',
'Some Name <[email protected]>',
],
});
});
Expand All @@ -184,22 +188,22 @@ describe('Validators', function () {
'Some Name <hans.m端[email protected]>',
'Some Name <hans@m端ller.com>',
'Some Name <test|123@m端ller.com>',
'Some Name <test[email protected]>',
'Some Name <some.name.midd.leNa.me.[email protected]>',
'Some Middle Name <some.name.midd.leNa.me.[email protected]>',
'Name <some.name.midd.leNa.me.[email protected]>',
'Name<some.name.midd.leNa.me.[email protected]>',
'Some Name <test123[email protected]>',
'Some Name <[email protected]>',
'Some Middle Name <[email protected]>',
'Name <[email protected]>',
'Name<[email protected]>',
],
invalid: [
'some.name.midd.leNa.me.[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'hans.m端[email protected]',
'hans@m端ller.com',
'test|123@m端ller.com',
'test[email protected]',
'test123[email protected]',
'invalidemail@',
'invalid.com',
'@invalid.com',
Expand Down
12 changes: 12 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ var default_email_options = {
/* 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](\.?[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;
Expand Down Expand Up @@ -218,6 +219,17 @@ function isEmail(str, options) {
should be done in normalizeEmail
*/
user = user.toLowerCase();

// Removing sub-address from username before gmail validation
var username = user.split('+')[0];

if (!isByteLength(username, { min: 6, max: 30 })) {
return false;
}

if (!gmailUserPart.test(username)) {
return false;
}
}

if (!isByteLength(user, { max: 64 }) || !isByteLength(domain, { max: 254 })) {
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 58cd58c

Please sign in to comment.