0

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

enter image description here

0

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.

15
  • I will try it now. Thank you so much. – Gurpreet Jan 13 at 14:33
  • letters-only code is not working. <input type="text" id="<?php echo $this->getFieldId('firstname')?>" name="<?php echo $this->getFieldName('firstname')?>" value="<?php echo $this->escapeHtml($this->getObject()->getFirstname()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->getStoreLabel('firstname')) ?>" maxlength="15" class="letters-only input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('firstname') ?>" <?php echo $this->getFieldParams() ?> /> </div> </div> Please let me know if I am doing any mistake – Gurpreet Jan 13 at 14:50
  • please see my updated, answer, and accept the answer, as this is all requirements that you want, thanks Munesh – Munesh Jalwani Jan 13 at 15:33
  • I do not have any module section in my code. – Gurpreet Jan 13 at 15:43
  • @Gurpreet if you want to set the limit on input field, then you should have to create the module, and do what i said, if you don't know how to create the module, then ill share the link reference that shows you to create the module. – Munesh Jalwani Jan 13 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.