Fixing Magento's Telephone Validation to Allow Dots and Slashes

Magento’s built-in telephone validation for customer accounts is fairly strict - it only allows digits, spaces, parentheses, plus signs and hyphens. That works for a lot of western formats but breaks for plenty of valid international phone numbers.

French phone numbers are commonly written with dots as separators (e.g. 06.76.40.32.22), and Austrian numbers often use a forward slash (e.g. +43680/2149568). Both of these formats are rejected by Magento’s Telephone validator out of the box.

The Telephone model validator in vendor/magento/module-customer/Model/Validator/Telephone.php uses a regex pattern that doesn’t account for dots or slashes:

private const PATTERN_TELEPHONE = '/(?:[\d\s\+\-\()]{1,20})/u';

This means any customer trying to save their phone number in a perfectly valid international format gets hit with “Invalid Phone Number” - not a great experience.

The issue is tracked internally as ACP2E-4554 and was discussed in magento/magento2#40544 and magento/magento2#40545. The fix was committed but if you’re on a version that doesn’t include it yet, here’s a patch.

Here’s an m2-hotfixes patch that adds support for dots and forward slashes in telephone validation:

--- a/vendor/magento/module-customer/Model/Validator/Telephone.php
+++ b/vendor/magento/module-customer/Model/Validator/Telephone.php
@@ -21,9 +21,11 @@
* \() :Matches open and close parentheses
* \+: Matches the plus sign.
* \-: Matches the hyphen.
+ * \.: Matches the dot character (e.g. 06.76.40.32.22)
+ * \/: Matches the forward slash (e.g. +43680/2149568)
* \d: Digits (0-9).
*/
- private const PATTERN_TELEPHONE = '/(?:[\d\s\+\-\()]{1,20})/u';
+ private const PATTERN_TELEPHONE = '/(?:[\d\s+().\/ -]{1,20})/u';

/**
* Validate telephone fields.
@@ -35,7 +37,7 @@
{
if (!$this->isValidTelephone($customer->getTelephone())) {
parent::_addMessages([[
- 'telephone' => "Invalid Phone Number. Please use 0-9, +, -, (, ) and space."
+ 'telephone' => "Invalid Phone Number. Please use 0-9, +, -, (, ), ., / and space."
]]);
}

Save this as m2-hotfixes/ac-13791__telephone-validation-dots-slashes.patch (or whatever naming convention you use for your hotfixes directory).

The updated regex pattern '/(?:[\d\s+().\/ -]{1,20})/u' adds . and / to the allowed character set.