How to Set Custom Validation Message in Magento 2
Magento 2 provides the admin to display custom form validation messages in multiple ways. It can be easily customized by altering the validator through a simple jQuery code.
Are you tired of your customers entering the wrong details in the user inputs? Are they entering numbers in the name fields or vice versa? If so, then you can easily prompt them to add valid details by adding a custom validation message in Magento 2. This will allow you to display a custom message in the front end when the user enters an invalid or blank input.
Magento 2 store owners can easily configure the Magento 2 form validation criteria and prompt the user to enter details accordingly.
For eg., on the user registration page, store owners can configure the settings to show a custom message to the customers in case of invalid input. The front end will prompt something like – “Please enter numbers only!” in case the customers enter any characters other than numbers in the phone number field.
Likewise, it can also be configured to show different messages on the basis of various conditions. Follow the below-mentioned steps to set custom validation message in Magento 2.
Steps to Set Custom Validation Message in Magento 2:
Just go to that input field and add a custom attribute “data-msg-(validator name) data-msg-(validator name)
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<form id="appointment-form" class="mt-form" data-mage-init='{"validation":{}}' enctype="multipart/form-data" method="post" name="mt-form"> <fieldset class="fieldset"> <div class="field required"> <label class="mtlabel" for="email">E-mail</label> <div class="control"> <input type="email" id="t_email" name="email" class="input-text" data-validate="{required:true}" data-msg-required="Please Enter A Valid Email Address" aria-required="true"> </div> </div> </fieldset> </form> |
That’s it.
You can refer to add validation in the custom password field in Magento 2.
Let me know through the comments if you have any confusion or problem while adding custom validation messages in Magento 2 store. It would be my pleasure to help you out with that.
Do share the solution with the Magento Community via social media.
Thank you!
Prev
Meetanshi Magento Christmas Offers 2021 – Amazing Deals on Magento Extensions
15+ Magento Statistics 2024 – Market Share, Usage, and More
Next