Domanda

How can i add validations for only alphabets in First name and Last name fields in Customer registration form?

enter image description here

È stato utile?

Soluzione

Extend name.phtml file in your theme as below and add letters-only validation:-

app\design\frontend\VendorName\themename\Magento_Customer\templates\widget\name.phtml

First Name:-

<input type="text" id="<?= $block->escapeHtmlAttr($block->getFieldId('firstname')) ?>"
               name="<?= $block->escapeHtmlAttr($block->getFieldName('firstname')) ?>"
               value="<?= $block->escapeHtmlAttr($block->getObject()->getFirstname()) ?>"
               title="<?= $block->escapeHtmlAttr($block->getStoreLabel('firstname')) ?>"
               class="letters-only input-text <?= $block->escapeHtmlAttr($block->getAttributeValidationClass('firstname')) ?>" <?php if ($block->getAttributeValidationClass('firstname') == 'required-entry') echo ' data-validate="{required:true}"' ?>>

Last Name:-

<input type="text" id="<?= $block->escapeHtmlAttr($block->getFieldId('lastname')) ?>"
               name="<?= $block->escapeHtmlAttr($block->getFieldName('lastname')) ?>"
               value="<?= $block->escapeHtmlAttr($block->getObject()->getLastname()) ?>"
               title="<?= $block->escapeHtmlAttr($block->getStoreLabel('lastname')) ?>"
               class="letters-only input-text <?= $block->escapeHtmlAttr($block->getAttributeValidationClass('lastname')) ?>" <?php if ($block->getAttributeValidationClass('lastname') == 'required-entry') echo ' data-validate="{required:true}"' ?>>

Edited: I saw you want to try the validation for field, so you can achieve that by using schema in your module, to update the existing field in database

app/code/VendorName/ModuleName/Setup

namespace Demo\StreetLimit\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

class UpgradeData implements UpgradeDataInterface
{
/**
 * @param EavSetup $eavSetupFactory
 */
public function __construct(
    \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
) {
    $this->customerSetupFactory = $customerSetupFactory;
}

/**
 * Upgrades customer_eav_attribute table for validate_rules to set limit on character for Street address for DHL module issue
 *
 * @param ModuleDataSetupInterface $setup
 * @param ModuleContextInterface $context
 * @return void
 */
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();
    /** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    $entityAttributes = [
        'customer_address' => [
            'street' => [
                'validate_rules' => '{"max_text_length":35,"min_text_length":2}'
            ],
        ],
    ];
    $this->upgradeAttributes($entityAttributes, $customerSetup);
    $setup->endSetup();
}

protected function upgradeAttributes(array $entityAttributes, \Magento\Customer\Setup\CustomerSetup $customerSetup)
{
    foreach ($entityAttributes as $entityType => $attributes) {
        foreach ($attributes as $attributeCode => $attributeData) {
            $attribute = $customerSetup->getEavConfig()->getAttribute($entityType, $attributeCode);
            foreach ($attributeData as $key => $value) {
                $attribute->setData($key, $value);
            }
            $attribute->save();
        }
    }
}

}

This is just demo for street address field, you can same use this or name's that you want for first and last name

Now letters-only validation only accept letter in first name and last name, not numeric value.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top