Skip to content

Commit e7b8557

Browse files
authored
Merge pull request validatorjs#769 from ProfNandaa/feat-741-is-phone-number-cc
isMobilePhone: add option for strictMode
2 parents ca0f25f + 5615a12 commit e7b8557

File tree

6 files changed

+32
-7
lines changed

6 files changed

+32
-7
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Validator | Description
100100
**isMACAddress(str)** | check if the string is a MAC address.
101101
**isMD5(str)** | check if the string is a MD5 hash.
102102
**isMimeType(str)** | check if the string matches to a valid [MIME type](https://en.wikipedia.org/wiki/Media_type) format
103-
**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', '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', '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).
103+
**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', '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', '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 `+`.
104104
**isMongoId(str)** | check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
105105
**isMultibyte(str)** | check if the string contains one or more multibyte chars.
106106
**isNumeric(str)** | check if the string contains only numbers.
@@ -153,8 +153,9 @@ In general, we follow the "fork-and-pull" Git workflow.
153153
4. Add changes to README.md if needed
154154
4. Commit changes to your own branch
155155
5. **Make sure** you merge the latest from "upstream" and resolve conflicts if there is any
156-
6. Push your work back up to your fork
157-
7. Submit a Pull request so that we can review your changes
156+
6. Repeat step 3(3) above
157+
7. Push your work back up to your fork
158+
8. Submit a Pull request so that we can review your changes
158159

159160
## Tests
160161

lib/isMobilePhone.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,11 @@ phones['en-CA'] = phones['en-US'];
8080
phones['fr-BE'] = phones['nl-BE'];
8181
phones['zh-HK'] = phones['en-HK'];
8282

83-
function isMobilePhone(str, locale) {
83+
function isMobilePhone(str, locale, options) {
8484
(0, _assertString2.default)(str);
85+
if (options && options.strictMode && !str.startsWith('+')) {
86+
return false;
87+
}
8588
if (locale in phones) {
8689
return phones[locale].test(str);
8790
} else if (locale === 'any') {

src/lib/isMobilePhone.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ phones['en-CA'] = phones['en-US'];
6969
phones['fr-BE'] = phones['nl-BE'];
7070
phones['zh-HK'] = phones['en-HK'];
7171

72-
export default function isMobilePhone(str, locale) {
72+
export default function isMobilePhone(str, locale, options) {
7373
assertString(str);
74+
if (options && options.strictMode && !str.startsWith('+')) {
75+
return false;
76+
}
7477
if (locale in phones) {
7578
return phones[locale].test(str);
7679
} else if (locale === 'any') {

test/validators.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4112,6 +4112,21 @@ describe('Validators', function () {
41124112
],
41134113
args: ['any'],
41144114
});
4115+
4116+
// strict mode
4117+
test({
4118+
validator: 'isMobilePhone',
4119+
valid: [
4120+
'+254728530234',
4121+
'+299 12 34 56',
4122+
],
4123+
invalid: [
4124+
'254728530234',
4125+
'0728530234',
4126+
'+728530234',
4127+
],
4128+
args: ['any', { strictMode: true }],
4129+
});
41154130
});
41164131

41174132
it('should validate currency', function () {

validator.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,8 +1033,11 @@ phones['en-CA'] = phones['en-US'];
10331033
phones['fr-BE'] = phones['nl-BE'];
10341034
phones['zh-HK'] = phones['en-HK'];
10351035

1036-
function isMobilePhone(str, locale) {
1036+
function isMobilePhone(str, locale, options) {
10371037
assertString(str);
1038+
if (options && options.strictMode && !str.startsWith('+')) {
1039+
return false;
1040+
}
10381041
if (locale in phones) {
10391042
return phones[locale].test(str);
10401043
} else if (locale === 'any') {

validator.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)