Skip to content

Commit 6be9634

Browse files
guspowerGus Power
and
Gus Power
authoredAug 3, 2023
feat(isEmail) extend to enable allow_underscores in domain (#2229)
* feat(isEmail) extend isEmail validator to enable allow_underscores option, allowing successful validation of email addresses containing domains with underscores * doc(isEmail) update README to list new email option allow_underscores --------- Co-authored-by: Gus Power <gus.power@bourne-leisure.co.uk>
1 parent 2f551c6 commit 6be9634

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Validator | Description
109109
**isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.<br/><br/>`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`.<br/><br/>`locale` determines the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa', 'fa-AF', 'fa-IR', 'fr-FR', 'fr-CA', 'hu-HU', 'id-ID', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pl-Pl', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN']`.<br/>**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'.
110110
**isDivisibleBy(str, number)** | check if the string is a number that is divisible by another.
111111
**isEAN(str)** | check if the string is an [EAN (European Article Number)][European Article Number].
112-
**isEmail(str [, options])** | check if the string is an email.<br/><br/>`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, domain_specific_validation: false, blacklisted_chars: '', host_blacklist: [] }`. 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, email addresses without a TLD in their domain will also be matched. If `ignore_max_length` is set to true, the validator will not check for the standard max length of an email. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by Gmail. If `blacklisted_chars` receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If `host_blacklist` is set to an array of strings and the part of the email after the `@` symbol matches one of the strings defined in it, the validation fails. If `host_whitelist` is set to an array of strings and the part of the email after the `@` symbol matches none of the strings defined in it, the validation fails.
112+
**isEmail(str [, options])** | check if the string is an email.<br/><br/>`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, allow_underscores: false, domain_specific_validation: false, blacklisted_chars: '', host_blacklist: [] }`. 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, email addresses without a TLD in their domain will also be matched. If `ignore_max_length` is set to true, the validator will not check for the standard max length of an email. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by Gmail. If `blacklisted_chars` receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If `host_blacklist` is set to an array of strings and the part of the email after the `@` symbol matches one of the strings defined in it, the validation fails. If `host_whitelist` is set to an array of strings and the part of the email after the `@` symbol matches none of the strings defined in it, the validation fails.
113113
**isEmpty(str [, options])** | check if the string has a length of zero.<br/><br/>`options` is an object which defaults to `{ ignore_whitespace: false }`.
114114
**isEthereumAddress(str)** | check if the string is an [Ethereum][Ethereum] address. Does not validate address checksums.
115115
**isFloat(str [, options])** | check if the string is a float.<br/><br/>`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 }`) it also has `locale` as an option.<br/><br/>`min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.<br/><br/>`locale` determines the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-CA', 'fr-FR', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`. Locale list is `validator.isFloatLocales`.

‎src/lib/isEmail.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import assertString from './util/assertString';
22

3-
import merge from './util/merge';
43
import isByteLength from './isByteLength';
54
import isFQDN from './isFQDN';
65
import isIP from './isIP';
6+
import merge from './util/merge';
77

88
const default_email_options = {
99
allow_display_name: false,
10+
allow_underscores: false,
1011
require_display_name: false,
1112
allow_utf8_local_part: true,
1213
require_tld: true,
@@ -142,6 +143,7 @@ export default function isEmail(str, options) {
142143
if (!isFQDN(domain, {
143144
require_tld: options.require_tld,
144145
ignore_max_length: options.ignore_max_length,
146+
allow_underscores: options.allow_underscores,
145147
})) {
146148
if (!options.allow_ip_domain) {
147149
return false;

‎test/validators.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe('Validators', () => {
3636
'invalid.com',
3737
'@invalid.com',
3838
'foo@bar.com.',
39+
'foo@_bar.com',
3940
'somename@gmail.com',
4041
'foo@bar.co.uk.',
4142
'z@co.c',
@@ -92,6 +93,16 @@ describe('Validators', () => {
9293
});
9394
});
9495

96+
it('should validate email addresses with underscores in the domain', () => {
97+
test({
98+
validator: 'isEmail',
99+
args: [{ allow_underscores: true }],
100+
valid: [
101+
'foobar@my_sarisari_store.typepad.com',
102+
],
103+
invalid: [],
104+
});
105+
});
95106

96107
it('should validate email addresses without UTF8 characters in local part', () => {
97108
test({

0 commit comments

Comments
 (0)
Please sign in to comment.