Skip to content

Commit 4d409bd

Browse files
aberbachriso
authored andcommittedNov 7, 2018
feat(isMobilePhone): add en-GH locale (Ghana) (#928)
* Update validators.js Add tests for Ghana phone codes * Update isMobilePhone.js Add Ghana phone number validation regex * Update README.md add Ghana phone code * Add Ghana phone number validation * remove README auto formatting
1 parent 209a801 commit 4d409bd

File tree

6 files changed

+57
-4
lines changed

6 files changed

+57
-4
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Validator | Description
106106
**isMACAddress(str)** | check if the string is a MAC address.<br/><br/>`options` is an object which defaults to `{no_colons: false}`. If `no_colons` is true, the validator will allow MAC addresses without the colons.
107107
**isMD5(str)** | check if the string is a MD5 hash.
108108
**isMimeType(str)** | check if the string matches to a valid [MIME type](https://en.wikipedia.org/wiki/Media_type) format
109-
**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-IQ', ar-JO', 'ar-KW', 'ar-SA', 'ar-SY', 'ar-TN', 'be-BY', 'bg-BG', 'bn-BD', 'cs-CZ', 'de-DE', 'da-DK', 'el-GR', 'en-AU', 'en-CA', 'en-GB', 'en-HK', 'en-IN', 'en-KE', 'en-MU', en-NG', 'en-NZ', 'en-RW', 'en-SG', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-PK', 'es-ES', 'es-MX', 'es-UY', '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', 'sl-SI', 'sk-SK', 'sr-RS', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-TW']` OR defaults to 'any'. If 'any' or a falsey value 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 `+`. Locale list is `validator.isMobilePhoneLocales`.
109+
**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-IQ', ar-JO', 'ar-KW', 'ar-SA', 'ar-SY', 'ar-TN', 'be-BY', 'bg-BG', 'bn-BD', 'cs-CZ', 'de-DE', 'da-DK', 'el-GR', 'en-AU', 'en-CA', 'en-GB', 'en-GH', 'en-HK', 'en-IN', 'en-KE', 'en-MU', en-NG', 'en-NZ', 'en-RW', 'en-SG', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-PK', 'es-ES', 'es-MX', 'es-UY', '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', 'sl-SI', 'sk-SK', 'sr-RS', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-TW']` OR defaults to 'any'. If 'any' or a falsey value 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 `+`. Locale list is `validator.isMobilePhoneLocales`.
110110
**isMongoId(str)** | check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
111111
**isMultibyte(str)** | check if the string contains one or more multibyte chars.
112112
**isNumeric(str [, options])** | check if the string contains only numbers.<br/><br/>`options` is an object which defaults to `{no_symbols: false}`. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`).

‎lib/isMobilePhone.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var phones = {
3030
'el-GR': /^(\+?30|0)?(69\d{8})$/,
3131
'en-AU': /^(\+?61|0)4\d{8}$/,
3232
'en-GB': /^(\+?44|0)7\d{9}$/,
33+
'en-GH': /^(\+233|0)(20|50|24|54|27|57|26|56|23|28)\d{7}$/,
3334
'en-HK': /^(\+?852\-?)?[456789]\d{3}\-?\d{4}$/,
3435
'en-IN': /^(\+?91|0)?[6789]\d{9}$/,
3536
'en-KE': /^(\+?254|0)?[7]\d{8}$/,

‎src/lib/isMobilePhone.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const phones = {
2020
'el-GR': /^(\+?30|0)?(69\d{8})$/,
2121
'en-AU': /^(\+?61|0)4\d{8}$/,
2222
'en-GB': /^(\+?44|0)7\d{9}$/,
23+
'en-GH': /^(\+233|0)(20|50|24|54|27|57|26|56|23|28)\d{7}$/,
2324
'en-HK': /^(\+?852\-?)?[456789]\d{3}\-?\d{4}$/,
2425
'en-IN': /^(\+?91|0)?[6789]\d{9}$/,
2526
'en-KE': /^(\+?254|0)?[7]\d{8}$/,

‎test/validators.js

+31
Original file line numberDiff line numberDiff line change
@@ -3673,6 +3673,37 @@ describe('Validators', () => {
36733673
'04123456789',
36743674
],
36753675
},
3676+
{
3677+
locale: 'en-GH',
3678+
valid: [
3679+
'0202345671',
3680+
'0502345671',
3681+
'0242345671',
3682+
'0542345671',
3683+
'0272345671',
3684+
'0572345671',
3685+
'0262345671',
3686+
'0562345671',
3687+
'0232345671',
3688+
'0282345671',
3689+
'+233202345671',
3690+
'+233502345671',
3691+
'+233242345671',
3692+
'+233542345671',
3693+
'+233272345671',
3694+
'+233572345671',
3695+
'+233262345671',
3696+
'+233562345671',
3697+
'+233232345671',
3698+
'+233282345671',
3699+
],
3700+
invalid: [
3701+
'082123',
3702+
'232345671',
3703+
'0292345671',
3704+
'+233292345671',
3705+
],
3706+
},
36763707
{
36773708
locale: 'en-HK',
36783709
valid: [

‎validator.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ function _nonIterableRest() {
5656
throw new TypeError("Invalid attempt to destructure non-iterable instance");
5757
}
5858

59+
function _toPrimitive(input, hint) {
60+
if (typeof input !== "object" || input === null) return input;
61+
var prim = input[Symbol.toPrimitive];
62+
63+
if (prim !== undefined) {
64+
var res = prim.call(input, hint || "default");
65+
if (typeof res !== "object") return res;
66+
throw new TypeError("@@toPrimitive must return a primitive value.");
67+
}
68+
69+
return (hint === "string" ? String : Number)(input);
70+
}
71+
72+
function _toPropertyKey(arg) {
73+
var key = _toPrimitive(arg, "string");
74+
75+
return typeof key === "symbol" ? key : String(key);
76+
}
77+
5978
function _addElementPlacement(element, placements, silent) {
6079
var keys = placements[element.placement];
6180

@@ -102,8 +121,8 @@ function _toElementDescriptor(elementObject) {
102121
throw new TypeError('An element descriptor\'s .kind property must be either "method" or' + ' "field", but a decorator created an element descriptor with' + ' .kind "' + kind + '"');
103122
}
104123

105-
var key = elementObject.key;
106-
if (typeof key !== "string" && typeof key !== "symbol") key = String(key);
124+
var key = _toPropertyKey(elementObject.key);
125+
107126
var placement = String(elementObject.placement);
108127

109128
if (placement !== "static" && placement !== "prototype" && placement !== "own") {
@@ -1362,6 +1381,7 @@ var phones = {
13621381
'el-GR': /^(\+?30|0)?(69\d{8})$/,
13631382
'en-AU': /^(\+?61|0)4\d{8}$/,
13641383
'en-GB': /^(\+?44|0)7\d{9}$/,
1384+
'en-GH': /^(\+233|0)(20|50|24|54|27|57|26|56|23|28)\d{7}$/,
13651385
'en-HK': /^(\+?852\-?)?[456789]\d{3}\-?\d{4}$/,
13661386
'en-IN': /^(\+?91|0)?[6789]\d{9}$/,
13671387
'en-KE': /^(\+?254|0)?[7]\d{8}$/,

‎validator.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.