Create an event in your custom module app/code/Vendor/Module/etc/events.xml
<event name="customer_address_save_before">
<observer name="telephone_no_customer_address_model_save_before" instance="Vendor\Module\Observer\Customer\ModelSaveBefore"/>
</event>
Create ModelSaveBefore.php
inside Vendor\Module\Observer\Customer
<?php
namespace Vendor\Module\Observer\Customer;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\CustomerFactory;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class ModelSaveBefore implements ObserverInterface
{
protected $customerFactory;
public function __construct(CustomerFactory $customerFactory)
{
$this->customerFactory = $customerFactory;
}
public function execute(Observer $observer)
{
$customerAddress = $observer->getCustomerAddress();
$telephone = $customerAddress->getTelephone();
$customerAddress->setTelephone(preg_replace("/[^a-zA-Z0-9]/", "", $telephone));
}
}
Run commands accordingly and get your work done
UPDATE: Using Plugin
Add in etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Vendor\Module\Plugin\Customer\ModelSaveBefore">
<plugin name="telephone_no_customer_address_model_save_before" type="Magento\Customer\Observer\BeforeAddressSaveObserver" sortOrder="10" disabled="false" />
</type>
</config>
Create Vendor\Module\Plugin\Customer\ModelSaveBefore.php
And try below code:
<?php
namespace Vendor\Module\Plugin\Customer;
use Magento\Customer\Observer\BeforeAddressSaveObserver
class ModelSaveBefore
{
public function beforeExecute(BeforeAddressSaveObserver $subject, $result)
{
$customerAddress = $result->getCustomerAddress();
$telephone = $customerAddress->getTelephone();
$customerAddress->setTelephone(preg_replace("/[^a-zA-Z0-9]/", "", $telephone));
return $result;
}
}