only alphabet in firstname and lastname in customer registration form Magento 2
-
26-02-2021 - |
Question
How can i add validations for only alphabets in First name and Last name fields in Customer registration form?
Solution
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}"' ?>>
Now letters-only validation only accept letter in first name and last name, not numeric value.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange