Check whitespaces and remove in the First Name Customer Registration in FE, BE

Once a new customer is being registered and Add New Customer in Admin the extension checks the First Name field. If the First Name field has whitespaces, they must be removed, so the customer entity is saved without whitespaces in the First Name property. All checks and modifications must be performed on the server side.

1

Create a new module and use plugin to achieve your requirement.
Create module with reigstration.php and module.xml and etc folder

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Model\ResourceModel\CustomerRepository">
        <plugin name="RemoveFirstNameWhiteSpace" type="JJ\Customer\Plugin\RemoveFirstNameWhiteSpace"/>
    </type>
</config>

Create the plugin folder and create a php file

<?php

namespace JJ\Customer\Plugin;

use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Model\ResourceModel\CustomerRepository;

class RemoveFirstNameWhiteSpace
{
    public function beforeSave(CustomerRepository $subject, CustomerInterface $customer, $passwordHash = null)
    {
    // Only apply to the new customer
    if (!$customer->getId() && $firstName = $customer->getFirstName()) {
        $firstName = str_replace(' ', '', $firstName);
        $customer->setFirstName($firstName);
    }

     return [$customer, $passwordHash];   
    }
}

Leave a comment

Your email address will not be published. Required fields are marked *