0

I need some help with UK GB Post Code Zip Validation Magento 2 Space

In that I want to force the user to enter spaces so its like this EC1A 1BB or W1A 0AX or M1 1AE so there must be space After the 2 3 or 4 charachter. so far I have spent several hours but not found any solution and only file i found is vendor/magento/module-directory/etc zip_codes.xml

At the moment it is accepting entry like this bt559zq which I dont want

Without this my shipping prices in table rates are not being calculated correctly

Need this setting wherever there is address for example customer accounts . one page checkout, cart shipping estimator

Can someone please help asap

thanks

0

You need to create a new module and add etc/zip_codes.xml with the following:

<?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="GB">
        <codes>
            <code id="pattern_1" active="true" example="AB12 3CD">^[a-zA-Z]{2}[0-9]{2}\s[0-9]{1}[a-zA-Z]{2}$</code>
            <code id="pattern_2" active="true" example="A1B 2CD">^[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
            <code id="pattern_3" active="true" example="AB1 2CD">^[a-zA-Z]{2}[0-9]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
            <code id="pattern_4" active="true" example="AB1C 2DF">^[a-zA-Z]{2}[0-9]{1}[a-zA-Z]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
            <code id="pattern_5" active="true" example="A12 3BC">^[a-zA-Z]{1}[0-9]{2}\s[0-9]{1}[a-zA-Z]{2}$</code>
            <code id="pattern_6" active="true" example="A1 2BC">^[a-zA-Z]{1}[0-9]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
        </codes>
    </zip>
</config>

These are pretty the same patterns to vendor/magento/module-directory/etc zip_codes.xml exclude a space is a mandatory now.

Also, you need to add Magento_Directory sequence into etc/module.xml file.

4
  • thank you for your reply can you clarify how i do this step – jt9489 Dec 22 '20 at 17:55
  • add Magento_Directory sequence into etc/module.xml file? – jt9489 Dec 23 '20 at 15:45
  • Updated the first comment. – Sergey Fedosimov Dec 23 '20 at 16:05
  • This is my understanding. 1 Create New Module in app/code/Postcodespace in there I put 1 file in app/code/Postcodespace\etc called zip_codes.xml with the code you gave above and second is the registration.php in app/code/Postcodespace But I am confused after this what do I do edit this one? vendor/magento/module-directory/etc zip_codes.xml or add something to the code you gave above, And what do i add? – jt9489 Dec 23 '20 at 16:14
0

Create app/code/Jt/Postcodespace/registration.php

<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Jt_Postcodespace',
    __DIR__
);

Create app/code/Jt/Postcodespace/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Jt_Postcodespace" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Directory" />
        </sequence>
    </module>
</config>

Create app/code/Jt/Postcodespace/etc/zip_codes.xml

<?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="GB">
        <codes>
            <code id="pattern_1" active="true" example="AB12 3CD">^[a-zA-Z]{2}[0-9]{2}\s[0-9]{1}[a-zA-Z]{2}$</code>
            <code id="pattern_2" active="true" example="A1B 2CD">^[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
            <code id="pattern_3" active="true" example="AB1 2CD">^[a-zA-Z]{2}[0-9]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
            <code id="pattern_4" active="true" example="AB1C 2DF">^[a-zA-Z]{2}[0-9]{1}[a-zA-Z]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
            <code id="pattern_5" active="true" example="A12 3BC">^[a-zA-Z]{1}[0-9]{2}\s[0-9]{1}[a-zA-Z]{2}$</code>
            <code id="pattern_6" active="true" example="A1 2BC">^[a-zA-Z]{1}[0-9]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
        </codes>
    </zip>
</config>

After that run in the app root dir in console:

php bin/magento setup:upgrade
1
  • Thank you for explaining and steps much appreciated it now shows the warning when post code is entered without space But I want to check is it possible to force the user to enter correct post code and not give them the option to continue and ignore the notice. As users tend to see the postcode letters and numbers are correct and are not where the space is they will keep ignoring – jt9489 Dec 24 '20 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.