How to perform custom validation in Magento 2

Every Business is different, that’s why each business has its own requirements. Being featured pack CMS, Magento allows the store owner to customize the way they can fulfill their business needs. Default, Magento allows you to build custom Extensions, CMS pages, block file and so on by giving freedom to give a personalized touch to your Store Layout. But sometimes, you need to set custom validation according to fulfill the business need for serving personalized experience or to comply with business requirements. Recently, we came across a requirement where the store admin was willing to add custom validation on Postcode the way customers can only enter 6 digits pin code not a bit small not a bit long. To do the same first we need to create a custom extension to perform JavaScript validation.
So, here is another blog series on “How to perform Custom Validation in Magento 2.”

First, we need to create registration.php file here at the following path.app\code\vendor\extension\registration.php

1

2

3

4

5

6

<?php

\Magento\Framework\Component\ComponentRegistrar::register(

\Magento\Framework\Component\ComponentRegistrar::MODULE,

'VENDOR_EXTENSION',

__DIR__

);

Now, we need to create module.xml file at below location.app\code\vendor\extension\etc\module.xml

Now its time to implement postcode validation rule to out checkout and for that purpose we need to create checkout_index_index.xml file at below location.app\code\VENDOR\EXTENSION\view\frontend\layout\checkout_index_index.xml

That’s it, hope this code will help you to add your Custom postcode validation in checkout field inside your Magento 2 store. You can even use & customize this code according to your need adding different types of customization to your store checkout.
Let us know if you are facing an issue while implementing using this code.
Happy Coding!

How to perform custom validation in Magento 2 2018-12-20T18:00:48+00:00 2018-12-20T18:00:48+00:00MageComp