Skip to content

Commit

Permalink
Merge pull request #742 from vipul-21/master
Browse files Browse the repository at this point in the history
Accepting array of locales for mobile phone validation
  • Loading branch information
chriso authored Jun 5, 2018
2 parents 0a9b52d + 29c4ff5 commit e81230d
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,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 [, 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', 'ar-TN', 'be-BY', 'bg-BG', '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', 'kk-KZ', 'ko-KR', 'lt-LT', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL', 'pt-PT', 'pt-BR', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'sv-SE', 'th-TH', '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 `+`.
**isMobilePhone(str, locale [, options])** | check if the string is a mobile phone number,<br/><br/>(locale is either an array of locales (e.g `['sk-SK', 'sr-RS']`) OR one of `['ar-AE', 'ar-DZ', 'ar-EG', 'ar-JO', 'ar-SA', 'ar-SY', 'ar-TN', 'be-BY', 'bg-BG', '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', 'kk-KZ', 'ko-KR', 'lt-LT', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL', 'pt-PT', 'pt-BR', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'sv-SE', 'th-TH', '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
12 changes: 11 additions & 1 deletion lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,17 @@ function isMobilePhone(str, locale, options) {
if (options && options.strictMode && !str.startsWith('+')) {
return false;
}
if (locale in phones) {
if (Array.isArray(locale)) {
return locale.some(function (key) {
if (phones.hasOwnProperty(key)) {
var phone = phones[key];
if (phone.test(str)) {
return true;
}
}
return false;
});
} else if (locale in phones) {
return phones[locale].test(str);
} else if (locale === 'any') {
for (var key in phones) {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,17 @@ export default function isMobilePhone(str, locale, options) {
if (options && options.strictMode && !str.startsWith('+')) {
return false;
}
if (locale in phones) {
if (Array.isArray(locale)) {
return locale.some((key) => {
if (phones.hasOwnProperty(key)) {
const phone = phones[key];
if (phone.test(str)) {
return true;
}
}
return false;
});
} else if (locale in phones) {
return phones[locale].test(str);
} else if (locale === 'any') {
for (const key in phones) {
Expand Down
43 changes: 34 additions & 9 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4158,6 +4158,35 @@ describe('Validators', () => {
'081234567891',
],
},
{
locale: ['en-ZA', 'be-BY'],
valid: [
'0821231234',
'+27821231234',
'27821231234',
'+375241234567',
'+375251234567',
'+375291234567',
'+375331234567',
'+375441234567',
'375331234567',
],
invalid: [
'082123',
'08212312345',
'21821231234',
'+21821231234',
'+0821231234',
'12345',
'',
'ASDFGJKLmZXJtZtesting123',
'010-38238383',
'+9676338855',
'19676338855',
'6676338855',
'+99676338855',
],
},
];

let allValid = [];
Expand All @@ -4167,15 +4196,11 @@ describe('Validators', () => {
if (fixture.valid) allValid = allValid.concat(fixture.valid);

if (Array.isArray(fixture.locale)) {
// for fixtures that are shared across multiple locales
// e.g. 'nb-NO' and 'nn-NO'
fixture.locale.forEach((locale) => {
test({
validator: 'isMobilePhone',
valid: fixture.valid,
invalid: fixture.invalid,
args: [locale],
});
test({
validator: 'isMobilePhone',
valid: fixture.valid,
invalid: fixture.invalid,
args: [fixture.locale],
});
} else {
test({
Expand Down
12 changes: 11 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,17 @@ function isMobilePhone(str, locale, options) {
if (options && options.strictMode && !str.startsWith('+')) {
return false;
}
if (locale in phones) {
if (Array.isArray(locale)) {
return locale.some(function (key) {
if (phones.hasOwnProperty(key)) {
var phone = phones[key];
if (phone.test(str)) {
return true;
}
}
return false;
});
} else if (locale in phones) {
return phones[locale].test(str);
} else if (locale === 'any') {
for (var key in phones) {
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit e81230d

Please sign in to comment.