From 38b9523b835b72bfe9f068b55015d8f7868951c9 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Tue, 16 Jan 2024 17:37:40 +0100 Subject: [PATCH] Themes: phone validation now knows about some short-codes --- .../InputElement/Validators/PhoneValidator.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/UI/InputElement/Validators/PhoneValidator.ts b/src/UI/InputElement/Validators/PhoneValidator.ts index f8a76ff221..6ef6a50270 100644 --- a/src/UI/InputElement/Validators/PhoneValidator.ts +++ b/src/UI/InputElement/Validators/PhoneValidator.ts @@ -35,6 +35,10 @@ export default class PhoneValidator extends Validator { if (country !== undefined) { countryCode = country()?.toUpperCase() } + if (this.isShortCode(str, countryCode)) { + return true + } + return parsePhoneNumberFromString(str, countryCode)?.isValid() ?? false } @@ -46,9 +50,28 @@ export default class PhoneValidator extends Validator { if (country) { countryCode = country() } + if (this.isShortCode(str, countryCode?.toUpperCase())) { + return str + } return parsePhoneNumberFromString( str, countryCode?.toUpperCase() as any )?.formatInternational() } + + /** + * Indicates if the given string is a special 'short code' valid in the given country + * see https://nl.wikipedia.org/wiki/Short_code + * @param str a possible phone number + * @param country the upper case, two-letter code for a country + * @private + */ + private isShortCode(str: string, country: string) { + if (country == "BE" && str.length === 4 && str.match(/[0-9]{4}/)) { + return true + } + if (country == "NL" && str.length === 4 && str.match(/14[0-9]{3}/)) { + return true + } + } }