Magento2 Validation – Implement Zip Code Validation

magento2uservalidationzipcode

In checkout page I try to add validation for postal code.

I already find a list with javascript validation for magento 2 here validation rule and I found this rule "validate-zip-international "but I don't know where to apply this rule.
In magento 1 you can add your javascript class in input class on billing.phtml.
Does anyone know what it's in Magento 2?

Now by defaul if a type a wrong postal code it only tell me that my code is wrong, and i can pass over this step(like in the next screen-shot)

enter image description here

Best Answer

To add validation on the zip field we should overwirte Magento_Checkout/layout/checkout_index_index.xml and add this code :

 <item name="validation" xsi:type="array">
    <item name="zip-range" xsi:type="string">true</item>
</item> 

zip-range for example is a already defined rule in rules.js.

We can overwrite this rule js according to our needs in this location:Theme\Theme\Magento_Ui\web\js\lib\validation\rules.js

Related Topic