I am going to cover in this post how to customise Magento checkout page zip code validation. In Magento it is very easy to customise zip code validation using regular expressions.
In default Magento all validations regular expressions defined in below file:
vendor/magento/module-directory/etc/zip_codes.xml
To customise zip code validation on checkout page for a specific country, you can customise as below shared steps:
- Create
zip_codes.xml
file toMM\Training\etc
folder, Here I have created my own module(MM_Training
). Make sure after creating module it should be enabled. - Add below code to
zip_codes.xml
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Directory:etc/zip_codes.xsd"> <zip countryCode="SG"> <codes> <code id="pattern_1" active="true" example="123456">^[0-9]{6}$</code> </codes> </zip> </config> |
In the given example I have covered SG country, with one pattern. If you require multiple patterns for a single country you can add multiple code nodes with more patterns.
Flush Magento cache and test your added new custom zip code validation on checkout page.
