{"id":1647,"date":"2021-03-17T07:35:32","date_gmt":"2021-03-17T07:35:32","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/add-custom-input-mask-for-zip-code-in-magento-2\/"},"modified":"2025-07-17T09:37:02","modified_gmt":"2025-07-17T04:07:02","slug":"add-custom-input-mask-for-zip-code-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-custom-input-mask-for-zip-code-in-magento-2\/","title":{"rendered":"How to Add Custom Input Mask for ZIP Code in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2\u00a0is a globally accepted and popular CMS used by merchants around the world to offer their products and services online. The platform offers the insane capability to customize the default features in order to prepare the store for a specific audience or global customers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Today, I\u2019ll talk about one such solution to customize the store\u2019s zip code validation for country-specific customers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The admin can add a custom zip code validator to the checkout page in Magento 2 store based on the countries they serve.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, the zip code example shown in the default message on the checkout page is the numeric value \u201c12345\u201d. However, countries like the Netherlands contain alphanumeric zip codes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hence, follow the below solution to&nbsp;<em><strong>add custom input mask for ZIP code in Magento 2<\/strong><\/em>&nbsp;store to show the example in alphanumeric format or any other validations based on the zip code format.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Add Custom Input Mask for ZIP Code in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><strong>zip_codes.xml<\/strong><\/em>&nbsp;at&nbsp;<strong>app\/code\/Vendor\/Module\/etc\/zip_codes.xml<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Directory:etc\/zip_codes.xsd\">\n    &lt;zip countryCode=\"GG\">\n        &lt;codes>\n            &lt;code id=\"pattern_1\" active=\"true\" example=\"AB1 2CD\">^[a-zA-Z]{2}[0-9]{1}\\s[0-9]{1}[a-zA-Z]{2}$&lt;\/code>\n        &lt;\/codes>\n    &lt;\/zip>\n    &lt;zip countryCode=\"NL\">\n        &lt;codes>\n            &lt;code id=\"pattern_1\" active=\"true\" example=\"1234BW\">^[0-9]{4}[a-zA-Z]{2}$&lt;\/code>\n            &lt;code id=\"pattern_2\" active=\"true\" example=\"1234 BW\">^[0-5]{4}[a-z]{2}$&lt;\/code>\n        &lt;\/codes>\n    &lt;\/zip>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to check delivery availability based on zip codes and restrict orders for invalid zip codes then explore our&nbsp;<a href=\"https:\/\/meetanshi.com\/magento-2-restrict-zip-codes.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 Restrict Zip Codes<\/a>&nbsp;Extension.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do share the post with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2\u00a0is a globally accepted and popular CMS used by merchants around the world to offer their products and services online. The platform offers the&#8230;<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1647","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1647"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1647\/revisions"}],"predecessor-version":[{"id":18173,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1647\/revisions\/18173"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}