Skip to content

Commit

Permalink
Allow requiring display names as part of email, optionally
Browse files Browse the repository at this point in the history
  • Loading branch information
icenine457 committed Nov 27, 2016
1 parent 4726231 commit 051ebd2
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Passing anything other than a string is an error.
- **isDate(str)** - check if the string is a date.
- **isDecimal(str)** - check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.
- **isDivisibleBy(str, number)** - check if the string is a number that's divisible by another.
- **isEmail(str [, options])** - check if the string is an email. `options` is an object which defaults to `{ allow_display_name: false, allow_utf8_local_part: true, require_tld: true }`. If `allow_display_name` is set to true, the validator will also match `Display Name <email-address>`. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, e-mail addresses without having TLD in their domain will also be matched.
- **isEmail(str [, options])** - check if the string is an email. `options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true }`. If `allow_display_name` is set to true, the validator will also match `Display Name <email-address>`. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name <email-address>`. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, e-mail addresses without having TLD in their domain will also be matched.
- **isEmpty(str)** - check if the string has a length of zero.
- **isFQDN(str [, options])** - check if the string is a fully qualified domain name (e.g. domain.com). `options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false }`.
- **isFloat(str [, options])** - check if the string is a float. `options` is an object which can contain the keys `min`, `max`, `gt`, and/or `lt` to validate the float is within boundaries (e.g. `{ min: 7.22, max: 9.55 }`). `min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.
Expand Down
2 changes: 2 additions & 0 deletions lib/alpha.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
var alpha = exports.alpha = {
'en-US': /^[A-Z]+$/i,
'cs-CZ': /^[A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,
'da-DK': /^[A-ZÆØÅ]+$/i,
'de-DE': /^[A-ZÄÖÜß]+$/i,
'es-ES': /^[A-ZÁÉÍÑÓÚÜ]+$/i,
'fr-FR': /^[A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,
Expand All @@ -24,6 +25,7 @@ var alpha = exports.alpha = {
var alphanumeric = exports.alphanumeric = {
'en-US': /^[0-9A-Z]+$/i,
'cs-CZ': /^[0-9A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,
'da-DK': /^[0-9A-ZÆØÅ]$/i,
'de-DE': /^[0-9A-ZÄÖÜß]+$/i,
'es-ES': /^[0-9A-ZÁÉÍÑÓÚÜ]+$/i,
'fr-FR': /^[0-9A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,
Expand Down
7 changes: 7 additions & 0 deletions lib/isEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

var default_email_options = {
allow_display_name: false,
require_display_name: false,
allow_utf8_local_part: true,
require_tld: true
};
Expand All @@ -43,10 +44,16 @@ function isEmail(str, options) {
(0, _assertString2.default)(str);
options = (0, _merge2.default)(options, default_email_options);

if (options.require_display_name) {
options.allow_display_name = true;
}

if (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;
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/lib/isEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import isFQDN from './isFQDN';

const default_email_options = {
allow_display_name: false,
require_display_name: false,
allow_utf8_local_part: true,
require_tld: true,
};
Expand All @@ -24,10 +25,16 @@ export default function isEmail(str, options) {
assertString(str);
options = merge(options, default_email_options);

if (options.require_display_name) {
options.allow_display_name = true;
}

if (options.allow_display_name) {
const display_email = str.match(displayName);
if (display_email) {
str = display_email[1];
} else if (options.require_display_name) {
return false;
}
}

Expand Down
47 changes: 47 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,53 @@ describe('Validators', function () {
});
});

it('should validate email addresses with required display names', function () {
test({
validator: 'isEmail',
args: [{ require_display_name: true }],
valid: [
'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 <[email protected]>',
'Some Name <[email protected]>',
'Some Middle Name <[email protected]>',
'Name <[email protected]>',
'Name<[email protected]>',
],
invalid: [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'hans.m端[email protected]',
'hans@m端ller.com',
'test|123@m端ller.com',
'[email protected]',
'invalidemail@',
'invalid.com',
'@invalid.com',
'[email protected].',
'[email protected].',
'Some Name <invalidemail@>',
'Some Name <invalid.com>',
'Some Name <@invalid.com>',
'Some Name <[email protected].>',
'Some Name <[email protected].>',
'Some Name [email protected].>',
'Some Name <[email protected].',
'Some Name < [email protected] >',
'Name [email protected]',
],
});
});


it('should validate URLs', function () {
test({
validator: 'isURL',
Expand Down
9 changes: 9 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@

var default_email_options = {
allow_display_name: false,
require_display_name: false,
allow_utf8_local_part: true,
require_tld: true
};
Expand All @@ -295,10 +296,16 @@
assertString(str);
options = merge(options, default_email_options);

if (options.require_display_name) {
options.allow_display_name = true;
}

if (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;
}
}

Expand Down Expand Up @@ -536,6 +543,7 @@
var alpha = {
'en-US': /^[A-Z]+$/i,
'cs-CZ': /^[A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,
'da-DK': /^[A-ZÆØÅ]+$/i,
'de-DE': /^[A-ZÄÖÜß]+$/i,
'es-ES': /^[A-ZÁÉÍÑÓÚÜ]+$/i,
'fr-FR': /^[A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,
Expand All @@ -554,6 +562,7 @@
var alphanumeric = {
'en-US': /^[0-9A-Z]+$/i,
'cs-CZ': /^[0-9A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,
'da-DK': /^[0-9A-ZÆØÅ]$/i,
'de-DE': /^[0-9A-ZÄÖÜß]+$/i,
'es-ES': /^[0-9A-ZÁÉÍÑÓÚÜ]+$/i,
'fr-FR': /^[0-9A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 051ebd2

Please sign in to comment.