Skip to content

Commit

Permalink
isMobilePhone: add option for strictMode
Browse files Browse the repository at this point in the history
This commit adds an option on isMobilePhone to allow for strict
strict checking of mobile phone numbers, i.e. must start with
'+<country-code>'

fixes #741
  • Loading branch information
profnandaa committed Dec 25, 2017
1 parent e241676 commit d92b200
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Validator | Description
**isMACAddress(str)** | check if the string is a MAC address.
**isMD5(str)** | check if the string is a MD5 hash.
**isMimeType(str)** | check if the string matches to a valid [MIME type](https://en.wikipedia.org/wiki/Media_type) format
**isMobilePhone(str, locale)** | check if the string is a mobile phone number,<br/><br/>(locale is one of `['ar-AE', 'ar-DZ','ar-EG', 'ar-JO', 'ar-SA', 'ar-SY', 'cs-CZ', 'de-DE', 'da-DK', 'el-GR', 'en-AU', 'en-CA', 'en-GB', 'en-HK', 'en-IN', 'en-KE', 'en-NG', 'en-NZ', 'en-RW', 'en-SG', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-PK', 'es-ES', 'et-EE', 'fa-IR', 'fi-FI', 'fr-FR', 'he-IL', 'hu-HU', 'it-IT', 'ja-JP', 'ko-KR', 'lt-LT', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL', 'pt-PT', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'tr-TR', 'uk-UA', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-TW']` OR 'any'. If 'any' is used, function will check if any of the locales match).
**isMobilePhone(str, locale, options)** | check if the string is a mobile phone number,<br/><br/>(locale is one of `['ar-AE', 'ar-DZ','ar-EG', 'ar-JO', 'ar-SA', 'ar-SY', 'cs-CZ', 'de-DE', 'da-DK', 'el-GR', 'en-AU', 'en-CA', 'en-GB', 'en-HK', 'en-IN', 'en-KE', 'en-NG', 'en-NZ', 'en-RW', 'en-SG', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-PK', 'es-ES', 'et-EE', 'fa-IR', 'fi-FI', 'fr-FR', 'he-IL', 'hu-HU', 'it-IT', 'ja-JP', 'ko-KR', 'lt-LT', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL', 'pt-PT', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'tr-TR', 'uk-UA', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-TW']` OR `'any'`. If `'any'` is used, function will check if any of the locales match).<br/><br/>`options` is an optional object that can be supplied with the following keys: `strictMode`, if this is set to `true`, the mobile phone number must be supplied with the country code and therefore must start with `+`.
**isMongoId(str)** | check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
**isMultibyte(str)** | check if the string contains one or more multibyte chars.
**isNumeric(str)** | check if the string contains only numbers.
Expand Down
5 changes: 4 additions & 1 deletion lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ phones['en-CA'] = phones['en-US'];
phones['fr-BE'] = phones['nl-BE'];
phones['zh-HK'] = phones['en-HK'];

function isMobilePhone(str, locale) {
function isMobilePhone(str, locale, options) {
(0, _assertString2.default)(str);
if (options && options.strictMode && !str.startsWith('+')) {
return false;
}
if (locale in phones) {
return phones[locale].test(str);
} else if (locale === 'any') {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ phones['en-CA'] = phones['en-US'];
phones['fr-BE'] = phones['nl-BE'];
phones['zh-HK'] = phones['en-HK'];

export default function isMobilePhone(str, locale) {
export default function isMobilePhone(str, locale, options) {
assertString(str);
if (options && options.strictMode && !str.startsWith('+')) {
return false;
}
if (locale in phones) {
return phones[locale].test(str);
} else if (locale === 'any') {
Expand Down
15 changes: 15 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3959,6 +3959,21 @@ describe('Validators', function () {
],
args: ['any'],
});

// strict mode
test({
validator: 'isMobilePhone',
valid: [
'+254728530234',
'+299 12 34 56',
],
invalid: [
'254728530234',
'0728530234',
'+728530234',
],
args: ['any', { strictMode: true }],
});
});

it('should validate currency', function () {
Expand Down
5 changes: 4 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1027,8 +1027,11 @@ phones['en-CA'] = phones['en-US'];
phones['fr-BE'] = phones['nl-BE'];
phones['zh-HK'] = phones['en-HK'];

function isMobilePhone(str, locale) {
function isMobilePhone(str, locale, options) {
assertString(str);
if (options && options.strictMode && !str.startsWith('+')) {
return false;
}
if (locale in phones) {
return phones[locale].test(str);
} else if (locale === 'any') {
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit d92b200

Please sign in to comment.