Skip to content

Commit

Permalink
Add validation for mobile numbers in isMobilePhone and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rnsnceman committed Nov 19, 2024
1 parent f54599c commit b1cb3a0
Show file tree
Hide file tree
Showing 2 changed files with 283 additions and 36 deletions.
24 changes: 17 additions & 7 deletions src/lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const phones = {
'ar-DZ': /^(\+?213|0)(5|6|7)\d{8}$/,
'ar-LB': /^(\+?961)?((3|81)\d{6}|7\d{7})$/,
'ar-EG': /^((\+?20)|0)?1[0125]\d{8}$/,
'ar-EH': /^(\+?212|0)[\s\-]?(5288|5289)[\s\-]?\d{5}$/,
'ar-IQ': /^(\+?964|0)?7[0-9]\d{8}$/,
'ar-JO': /^(\+?962|0)?7[789]\d{7}$/,
'ar-KW': /^(\+?965)([569]\d{7}|41\d{6})$/,
Expand All @@ -19,6 +20,7 @@ const phones = {
'ar-SD': /^((\+?249)|0)?(9[012369]|1[012])\d{7}$/,
'ar-SY': /^(!?(\+?963)|0)?9\d{8}$/,
'ar-TN': /^(\+?216)?[2459]\d{7}$/,
'ar-YE': /^(((\+|00)9677|0?7)[0137]\d{7}|((\+|00)967|0)[1-7]\d{6})$/,
'az-AZ': /^(\+994|0)(10|5[015]|7[07]|99)\d{7}$/,
'bs-BA': /^((((\+|00)3876)|06))((([0-3]|[5-6])\d{6})|(4\d{7}))$/,
'be-BY': /^(\+?375)?(24|25|29|33|44)\d{7}$/,
Expand All @@ -30,15 +32,19 @@ const phones = {
'de-DE': /^((\+49|0)1)(5[0-25-9]\d|6([23]|0\d?)|7([0-57-9]|6\d))\d{7,9}$/,
'de-AT': /^(\+43|0)\d{1,4}\d{3,12}$/,
'de-CH': /^(\+41|0)([1-9])\d{1,9}$/,
'de-LI': /^(?:\+423|423)[\s]?(2\d{6}|[67]\d{6}|800\d{5}|90\d{5})$/,
'de-LU': /^(\+352)?((6\d1)\d{6})$/,
'dv-MV': /^(\+?960)?(7[2-9]|9[1-9])\d{5}$/,
'dz-BT': /^(\+?975|0)?(17|16|77|02)\d{6}$/,
'el-GR': /^(\+?30|0)?6(8[5-9]|9(?![26])[0-9])\d{7}$/,
'el-CY': /^(\+?357?)?(9(9|6)\d{6})$/,
'en-AI': /^(\+?1|0)264(?:2(35|92)|4(?:6[1-2]|76|97)|5(?:3[6-9]|8[1-4])|7(?:2(4|9)|72))\d{4}$/,
'en-AU': /^(\+?61|0)4\d{8}$/,
'en-AG': /^(?:\+1|1)268(?:464|7(?:1[3-9]|[28]\d|3[0246]|64|7[0-689]))\d{4}$/,
'en-BB': /^(?:\+1[-\s]?|011[-\s]?(?:1[-\s]?)?|1[-\s]?)(?:246|\(246\))[-\s]?\d{3}[-\s]?\d{4}$/,
'en-BM': /^(\+?1)?441(((3|7)\d{6}$)|(5[0-3][0-9]\d{4}$)|(59\d{5}$))/,
'en-BS': /^(\+?1[-\s]?|0)?\(?242\)?[-\s]?\d{3}[-\s]?\d{4}$/,
'en-BZ': /^(\+?501)[2-8]\d{6}$/,
'en-GB': /^(\+?44|0)7[1-9]\d{8}$/,
'en-GG': /^(\+?44|0)1481\d{6}$/,
'en-GH': /^(\+233|0)(20|50|24|54|27|57|26|56|23|53|28|55|59)\d{7}$/,
Expand Down Expand Up @@ -66,9 +72,12 @@ const phones = {
'en-RW': /^(\+?250|0)?[7]\d{8}$/,
'en-SG': /^(\+65)?[3689]\d{7}$/,
'en-SL': /^(\+?232|0)\d{8}$/,
'en-SR': /^(\+?597)[1-9]\d{6}$/,
'en-TZ': /^(\+?255|0)?[67]\d{8}$/,
'en-UG': /^(\+?256|0)?[7]\d{8}$/,
'en-US': /^((\+1|1)?( |-)?)?(\([2-9][0-9]{2}\)|[2-9][0-9]{2})( |-)?([2-9][0-9]{2}( |-)?[0-9]{4})$/,
'en-VU': /^\+?678\s?5\d{5}$/,
'en-WS': /^(?:\+?685\s?)?(?:2\d{4}|[347]\d{5,6}|8\d{6}|5\d{6,7})$/,
'en-ZA': /^(\+?27|0)\d{9}$/,
'en-ZM': /^(\+?26)?0[79][567]\d{7}$/,
'en-ZW': /^(\+263)[0-9]{9}$/,
Expand All @@ -93,13 +102,15 @@ const phones = {
'es-UY': /^(\+598|0)9[1-9][\d]{6}$/,
'es-VE': /^(\+?58)?(2|4)\d{9}$/,
'et-EE': /^(\+?372)?\s?(5|8[1-4])\s?([0-9]\s?){6,7}$/,
'fa-AF': /^(\+93|0)?(2{1}[0-8]{1}|[3-5]{1}[0-4]{1})(\d{7})$/,
'fa-IR': /^(\+?98[\-\s]?|0)9[0-39]\d[\-\s]?\d{3}[\-\s]?\d{4}$/,
'fi-FI': /^(\+?358|0)\s?(4[0-6]|50)\s?(\d\s?){4,8}$/,
'fj-FJ': /^(\+?679)?\s?\d{3}\s?\d{4}$/,
'fo-FO': /^(\+?298)?\s?\d{2}\s?\d{2}\s?\d{2}$/,
'fr-BF': /^(\+226|0)[67]\d{7}$/,
'fr-BJ': /^(\+229)\d{8}$/,
'fr-CD': /^(\+?243|0)?(8|9)\d{8}$/,
'fr-MC': /^(?:\+377|377)[\s]?(?:[3469]\d{6,7}|800\d{4,5})$/,
'fr-CM': /^(\+?237)6[0-9]{8}$/,
'fr-FR': /^(\+?33|0)[67]\d{8}$/,
'fr-GF': /^(\+?594|0|00594)[67]\d{8}$/,
Expand All @@ -112,13 +123,15 @@ const phones = {
'hu-HU': /^(\+?36|06)(20|30|31|50|70)\d{7}$/,
'id-ID': /^(\+?62|0)8(1[123456789]|2[1238]|3[1238]|5[12356789]|7[78]|9[56789]|8[123456789])([\s?|\d]{5,11})$/,
'ir-IR': /^(\+98|0)?9\d{9}$/,
'is-IS': /^(\+?354)?\d{7}$/,
'it-IT': /^(\+?39)?\s?3\d{2} ?\d{6,7}$/,
'it-SM': /^((\+378)|(0549)|(\+390549)|(\+3780549))?6\d{5,9}$/,
'ja-JP': /^(\+81[ \-]?(\(0\))?|0)[6789]0[ \-]?\d{4}[ \-]?\d{4}$/,
'ka-GE': /^(\+?995)?(79\d{7}|5\d{8})$/,
'kk-KZ': /^(\+?7|8)?7\d{9}$/,
'kl-GL': /^(\+?299)?\s?\d{2}\s?\d{2}\s?\d{2}$/,
'ko-KR': /^((\+?82)[ \-]?)?0?1([0|1|6|7|8|9]{1})[ \-]?\d{3,4}[ \-]?\d{4}$/,
'ko-KP': /^(\+850|850)\d{7,8}$/,
'ko-KR': /^\+(?:82)?[- ]?(?:0?[1-9]{2}|[1-9]{3})[- ]?\d{4}[- ]?\d{4}$|^\d{2,3}[- ]?\d{4}[- ]?\d{4}$/,
'ky-KG': /^(\+996\s?)?(22[0-9]|50[0-9]|55[0-9]|70[0-9]|75[0-9]|77[0-9]|880|990|995|996|997|998)\s?\d{3}\s?\d{3}$/,
'lt-LT': /^(\+370|8)\d{8}$/,
'lv-LV': /^(\+?371)2\d{7}$/,
Expand Down Expand Up @@ -151,15 +164,12 @@ const phones = {
'th-TH': /^(\+66|66|0)\d{9}$/,
'tr-TR': /^(\+?90|0)?5\d{9}$/,
'tk-TM': /^(\+993|993|8)\d{8}$/,
'to-TO': /^(?:\+?676 ?)[1-8]\d{3,5}$/,
'uk-UA': /^(\+?38)?0(50|6[36-8]|7[357]|9[1-9])\d{7}$/,
'uz-UZ': /^(\+?998)?(6[125-79]|7[1-69]|88|9\d)\d{7}$/,
'vi-VN': /^((\+?84)|0)((3([2-9]))|(5([25689]))|(7([0|6-9]))|(8([1-9]))|(9([0-9])))([0-9]{7})$/,
'zh-CN': /^((\+|00)86)?(1[3-9]|9[28])\d{9}$/,
'zh-TW': /^(\+?886\-?|0)?9\d{8}$/,
'dz-BT': /^(\+?975|0)?(17|16|77|02)\d{6}$/,
'ar-YE': /^(((\+|00)9677|0?7)[0137]\d{7}|((\+|00)967|0)[1-7]\d{6})$/,
'ar-EH': /^(\+?212|0)[\s\-]?(5288|5289)[\s\-]?\d{5}$/,
'fa-AF': /^(\+93|0)?(2{1}[0-8]{1}|[3-5]{1}[0-4]{1})(\d{7})$/,
};
/* eslint-enable max-len */

Expand Down Expand Up @@ -190,10 +200,10 @@ export default function isMobilePhone(str, locale, options) {
}
return false;
});
} else if (locale in phones) {
} if (locale in phones) {
return phones[locale].test(str);
// alias falsey locale as 'any'
} else if (!locale || locale === 'any') {
} if (!locale || locale === 'any') {
for (const key in phones) {
// istanbul ignore else
if (phones.hasOwnProperty(key)) {
Expand Down
Loading

0 comments on commit b1cb3a0

Please # to comment.