Validate Name Field in Magento 2
In the process of development, we receive various data from users. This data needs to be checked for correct input. The validity of the data entered by the user can be checked via default Magento 2 tools.
We can also extend the set of validation rules. Let’s consider how it’s performed.
I want to add validation only to accept letters not numeric in the first name and last name. Below is the reference code:
Navigation:Home> Login> My Account> Address Book>
C:\wamp\www\project folder\lib\web\mage\validation.js
'letters-only':
[
function (value, element) {
return this.optional(element) || /^[a-z]+$/i.test(value);
},
$.mage.__('Letters only please')
],
C:\wamp\www\project folder\app\design\frontend\Mgs\mgsblank\Magento_Customer\templates\widget\name.phtml
And add in your html file ?>
Class=”letters-only”
Validate-name -> For charecter validation.
Required-entry -> Field should not be null.
After that
-> Php bin/magento setup:upgrade //(MUST)
-> Php bin/magento setup:static-content:deploy //(MUST)
//Important Section
-> Php bin/magento cache:clean
-> Php bin/magento cache:flush
Conclusion:
You can use the code in Magento development. Please share your review in the comment section.
Comments