From 60a1b893e37ecf50364db5bd31f1764744a48d50 Mon Sep 17 00:00:00 2001 From: tjt253856563-glitch Date: Sat, 17 Oct 2026 20:39:41 +0800 Subject: [PATCH] feat(isMobilePhone): add en-TK (Tokelau) validation --- README.md | 2 +- src/lib/isMobilePhone.js | 2 ++ test/issue1761.test.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/issue1761.test.js diff --git a/README.md b/README.md index e8b7aedd9..756e392a0 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ Validator | Description **isMailtoURI(str, [, options])** | check if the string is a [Mailto URI format][Mailto URI Format].

`options` is an object of validating emails inside the URI (check `isEmail`s options for details). **isMD5(str)** | check if the string is a MD5 hash.

Please note that you can also use the `isHash(str, 'md5')` function. Keep in mind that MD5 has some collision weaknesses compared to other algorithms (e.g., SHA). **isMimeType(str)** | check if the string matches to a valid [MIME type][MIME Type] format. -**isMobilePhone(str [, locale [, options]])** | check if the string is a mobile phone number,

`locale` is either an array of locales (e.g. `['sk-SK', 'sr-RS']`) OR one of `['am-AM', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-LB', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LY', 'ar-MA', 'ar-OM', 'ar-PS', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'az-AZ', 'ar-QA', 'bs-BA', 'be-BY', 'bg-BG', 'bn-BD', 'ca-AD', 'cs-CZ', 'da-DK', 'de-DE', 'de-AT', 'de-CH', 'de-LU', 'dv-MV', 'el-GR', 'el-CY', 'en-AI', 'en-AU', 'en-AG', 'en-BM', 'en-BS', 'en-GB', 'en-GG', 'en-GH', 'en-GY', 'en-HK', 'en-MO', 'en-IE', 'en-IN', 'en-JM', 'en-KE', 'fr-CF', 'en-SS', 'en-KI', 'en-KN', 'en-LS', 'en-MT', 'en-MU', 'en-MW', 'en-NA', 'en-NG', 'en-NZ', 'en-PG', 'en-PK', 'en-PH', 'en-RW', 'en-SG', 'en-SL', 'en-TZ', 'en-UG', 'en-US', 'en-ZA', 'en-ZM', 'en-ZW', 'en-BW', 'es-AR', 'es-BO', 'es-CO', 'es-CL', 'es-CR', 'es-CU', 'es-DO', 'es-HN', 'es-EC', 'es-ES', 'es-GT', 'es-PE', 'es-MX', 'es-NI', 'es-PA', 'es-PY', 'es-SV', 'es-UY', 'es-VE', 'et-EE', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BF', 'fr-BJ', 'fr-CD', 'fr-CM', 'fr-DJ', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-PF', 'fr-RE', 'fr-WF', 'he-IL', 'hu-HU', 'id-ID', 'ir-IR', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'ky-KG', 'lt-LT', 'lv-LV', 'mg-MG', 'mn-MN', 'my-MM', 'ms-MY', 'mz-MZ', 'nb-NO', 'ne-NP', 'nl-BE', 'nl-NL', 'nl-AW', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'pt-AO', 'ro-MD', 'ro-RO', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'so-SO', 'sq-AL', 'sr-RS', 'sv-SE', 'tg-TJ', 'th-TH', 'tr-TR', 'tk-TM', 'uk-UA', 'uz-UZ', 'vi-VN', 'zh-CN', 'zh-TW', 'dz-BT', 'ar-YE', 'ar-EH', 'fa-AF', 'mk-MK', 'en-CA', 'fr-CA', 'fr-BE', 'zh-HK', 'zh-MO', 'ga-IE', 'fr-CH', 'it-CH', 'en-CM']` OR defaults to `'any'`. If `any` or a falsey value is used, the function will check if any of the locales match.

`options` is an optional object that can be supplied with the following key: `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`. +**isMobilePhone(str [, locale [, options]])** | check if the string is a mobile phone number,

`locale` is either an array of locales (e.g. `['sk-SK', 'sr-RS']`) OR one of `['am-AM', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-LB', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LY', 'ar-MA', 'ar-OM', 'ar-PS', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'az-AZ', 'ar-QA', 'bs-BA', 'be-BY', 'bg-BG', 'bn-BD', 'ca-AD', 'cs-CZ', 'da-DK', 'de-DE', 'de-AT', 'de-CH', 'de-LU', 'dv-MV', 'el-GR', 'el-CY', 'en-AI', 'en-AU', 'en-AG', 'en-BM', 'en-BS', 'en-GB', 'en-GG', 'en-GH', 'en-GY', 'en-HK', 'en-MO', 'en-IE', 'en-IN', 'en-JM', 'en-KE', 'fr-CF', 'en-SS', 'en-KI', 'en-KN', 'en-LS', 'en-MT', 'en-MU', 'en-MW', 'en-NA', 'en-NG', 'en-NZ', 'en-PG', 'en-PK', 'en-PH', 'en-RW', 'en-SG', 'en-TK', 'en-SL', 'en-TZ', 'en-UG', 'en-US', 'en-ZA', 'en-ZM', 'en-ZW', 'en-BW', 'es-AR', 'es-BO', 'es-CO', 'es-CL', 'es-CR', 'es-CU', 'es-DO', 'es-HN', 'es-EC', 'es-ES', 'es-GT', 'es-PE', 'es-MX', 'es-NI', 'es-PA', 'es-PY', 'es-SV', 'es-UY', 'es-VE', 'et-EE', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BF', 'fr-BJ', 'fr-CD', 'fr-CM', 'fr-DJ', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-PF', 'fr-RE', 'fr-WF', 'he-IL', 'hu-HU', 'id-ID', 'ir-IR', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'ky-KG', 'lt-LT', 'lv-LV', 'mg-MG', 'mn-MN', 'my-MM', 'ms-MY', 'mz-MZ', 'nb-NO', 'ne-NP', 'nl-BE', 'nl-NL', 'nl-AW', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'pt-AO', 'ro-MD', 'ro-RO', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'so-SO', 'sq-AL', 'sr-RS', 'sv-SE', 'tg-TJ', 'th-TH', 'tr-TR', 'tk-TM', 'uk-UA', 'uz-UZ', 'vi-VN', 'zh-CN', 'zh-TW', 'dz-BT', 'ar-YE', 'ar-EH', 'fa-AF', 'mk-MK', 'en-CA', 'fr-CA', 'fr-BE', 'zh-HK', 'zh-MO', 'ga-IE', 'fr-CH', 'it-CH', 'en-CM']` OR defaults to `'any'`. If `any` or a falsey value is used, the function will check if any of the locales match.

`options` is an optional object that can be supplied with the following key: `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`. **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 [, options])** | check if the string contains only numbers.

`options` is an object which defaults to `{ no_symbols: false }` it also has `locale` as an option. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`).

`locale` determines the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fr-FR', 'fr-CA', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`. diff --git a/src/lib/isMobilePhone.js b/src/lib/isMobilePhone.js index 32379bde5..b6e7f313d 100644 --- a/src/lib/isMobilePhone.js +++ b/src/lib/isMobilePhone.js @@ -66,6 +66,8 @@ const phones = { 'en-PH': /^(09|\+639)\d{9}$/, 'en-RW': /^(\+?250|0)?[7]\d{8}$/, 'en-SG': /^(\+65)?[3689]\d{7}$/, + // ITU Tokelau numbering plan, communication of 6 December 2015. + 'en-TK': /^(\+?690)?7[2-4]\d{2,5}$/, 'en-SL': /^(\+?232|0)\d{8}$/, 'en-TZ': /^(\+?255|0)?[67]\d{8}$/, 'en-UG': /^(\+?256|0)?[7]\d{8}$/, diff --git a/test/issue1761.test.js b/test/issue1761.test.js new file mode 100644 index 000000000..92a5e4d24 --- /dev/null +++ b/test/issue1761.test.js @@ -0,0 +1,32 @@ +import assert from 'assert'; +import isMobilePhone, { locales } from '../src/lib/isMobilePhone'; + +describe('Issue1761 Tokelau mobile phone validation', () => { + it('accepts all assigned prefixes and national length boundaries', () => { + ['72', '73', '74'].forEach((prefix) => { + ['00', '000', '0000', '00000'].forEach((suffix) => { + ['', '690', '+690'].forEach((country) => { + const value = country + prefix + suffix; + assert.strictEqual(isMobilePhone(value, 'en-TK'), true, value); + }); + }); + }); + }); + + it('rejects incorrect lengths, unassigned prefixes and formatting', () => { + ['+690720', '+69072000000', '720', '72000000', '+6907100', '+6907500', + '+6903101', '+6917200', '+69072ab', '+690 7200', '07200', '7200\n'].forEach((value) => { + assert.strictEqual(isMobilePhone(value, 'en-TK'), false, JSON.stringify(value)); + }); + }); + + it('supports strictMode, locale arrays, any and the locale list', () => { + assert.strictEqual(isMobilePhone('+6907200', 'en-TK', { strictMode: true }), true); + assert.strictEqual(isMobilePhone('6907200', 'en-TK', { strictMode: true }), false); + assert.strictEqual(isMobilePhone('7200', 'en-TK', { strictMode: true }), false); + assert.strictEqual(isMobilePhone('+6907400000', ['en-US', 'en-TK']), true); + assert.strictEqual(isMobilePhone('+6907300', 'any'), true); + assert.strictEqual(isMobilePhone('+6907300'), true); + assert.strictEqual(locales.includes('en-TK'), true); + }); +});