Skip to content

Commit 66ac635

Browse files
committed
Merge pull request #460 from chriso/chriso/configurable-email-normalization
Configurable GMail address normalization
2 parents 82ed00f + a856995 commit 66ac635

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ $ bower install validator-js
8181
- **blacklist(input, chars)** - remove characters that appear in the blacklist. The characters are used in a RegExp and so you will need to escape some chars, e.g. `blacklist(input, '\\[\\]')`.
8282
- **escape(input)** - replace `<`, `>`, `&`, `'`, `"` and `/` with HTML entities.
8383
- **ltrim(input [, chars])** - trim characters from the left-side of the input.
84-
- **normalizeEmail(email [, options])** - canonicalize an email address. `options` is an object which defaults to `{ lowercase: true }`. With `lowercase` set to `true`, the local part of the email address is lowercased for all domains; the hostname is always lowercased and the local part of the email address is always lowercased for hosts that are known to be case-insensitive (currently only GMail). Normalization follows special rules for known providers: currently, GMail addresses have dots removed in the local part and are stripped of tags (e.g. `[email protected]` becomes `[email protected]`) and all `@googlemail.com` addresses are normalized to `@gmail.com`.
84+
- **normalizeEmail(email [, options])** - canonicalize an email address. `options` is an object which defaults to `{ lowercase: true, remove_dots: true, remove_extension: true }`. With `lowercase` set to `true`, the local part of the email address is lowercased for all domains; the hostname is always lowercased and the local part of the email address is always lowercased for hosts that are known to be case-insensitive (currently only GMail). Normalization follows special rules for known providers: currently, GMail addresses have dots removed in the local part and are stripped of tags (e.g. `[email protected]` becomes `[email protected]`) and all `@googlemail.com` addresses are normalized to `@gmail.com`.
8585
- **rtrim(input [, chars])** - trim characters from the right-side of the input.
8686
- **stripLow(input [, keep_new_lines])** - remove characters with a numerical value < 32 and 127, mostly control characters. If `keep_new_lines` is `true`, newline characters are preserved (`\n` and `\r`, hex `0xA` and `0xD`). Unicode-safe in JavaScript.
8787
- **toBoolean(input [, strict])** - convert the input to a boolean. Everything except for `'0'`, `'false'` and `''` returns `true`. In strict mode only `'1'` and `'true'` return `true`.

test/sanitizers.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ describe('Sanitizers', function () {
198198
, 'an invalid email address': false
199199
, '': false
200200
, '[email protected]': false
201-
// [email protected] was removed from test cases because of a bug with validator.isEmail. See issue #258
201+
, '[email protected]': false
202+
, '[email protected]': false
203+
202204
}
203205
});
204206
test({
@@ -211,13 +213,27 @@ describe('Sanitizers', function () {
211213
212214
213215
214-
216+
215217
// Domains that are known for being case-insensitive are always lowercased
216218
217219
218220
219221
}
220222
});
223+
test({
224+
sanitizer: 'normalizeEmail'
225+
, args: [{remove_dots: false}]
226+
, expect: {
227+
228+
}
229+
});
230+
test({
231+
sanitizer: 'normalizeEmail'
232+
, args: [{remove_extension: false}]
233+
, expect: {
234+
235+
}
236+
});
221237
});
222238

223239
});

validator.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,9 @@
777777
};
778778

779779
var default_normalize_email_options = {
780-
lowercase: true
780+
lowercase: true,
781+
remove_dots: true,
782+
remove_extension: true
781783
};
782784

783785
validator.normalizeEmail = function (email, options) {
@@ -788,11 +790,16 @@
788790
var parts = email.split('@', 2);
789791
parts[1] = parts[1].toLowerCase();
790792
if (parts[1] === 'gmail.com' || parts[1] === 'googlemail.com') {
791-
parts[0] = parts[0].toLowerCase().replace(/\./g, '');
792-
if (parts[0][0] === '+') {
793+
if (options.remove_extension) {
794+
parts[0] = parts[0].split('+')[0];
795+
}
796+
if (options.remove_dots) {
797+
parts[0] = parts[0].replace(/\./g, '');
798+
}
799+
if (!parts[0].length) {
793800
return false;
794801
}
795-
parts[0] = parts[0].split('+')[0];
802+
parts[0] = parts[0].toLowerCase();
796803
parts[1] = 'gmail.com';
797804
} else if (options.lowercase) {
798805
parts[0] = parts[0].toLowerCase();

0 commit comments

Comments
 (0)