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:

That’s it.

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!

5
(based on 6 Reviews)
How to Set Custom Validation Message in Magento 2

Kishor Chhugani

He is a Magento developer at Meetanshi with a jolly, extrovert, & friendly personality. He loves playing cricket and is a passionate foodie!

Leave a Reply

Your email address will not be published. Required fields are marked *