0

I want to validate the post code exact 6 digit. Means user can not add more than or less than 6 digits. And this I need in checkout page of Magento 2.

Thanks in advance.

0

Please try this, It's work for me

Create new validation rule

Create registration.php file here app/code/RS/Validation/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'RS_Validation',
    __DIR__
);

Create module.xml file here app/code/RS/Validation/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="RS_Validation" setup_version="1.0.0">
    </module>
</config>

Create rsvalidation.js file here app/code/RS/Validation/view/frontend/web/js/rsvalidation.js

define([
   'Magento_Ui/js/lib/validation/validator',
   'jquery',
   'jquery/ui',
   'jquery/validate',
   'mage/translate'
], function(validator,$){ 
    'use strict';

    return function() {
        $.validator.addMethod(
        'rspostcode', function (value, element) { 
        console.log(value.length);
        return value.length > 5 &&  value.length < 7 && value.match(/[0-9]/);
        }, $.mage.__('Input 6 digit postcode')); 

        validator.addRule(
        'rspostcode', function (value, element) { 
        console.log(value.length);
        return value.length > 5 &&  value.length < 7 && value.match(/[0-9]/);
        },$.mage.__('Input 6 digit postcode')
        );
    }
});

Create requirejs-config.js file here app/code/RS/Validation/view/frontend/requirejs-config.js

var config = {
    "map": {
       "*": {
           "rsvalidation": "RS_Validation/js/rsvalidation"
       }
   }
};

Add validation rule to checkout postcode create file checkout_index_index.xml file here app/code/RS/Validation/view/frontend/layout/checkout_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="checkout" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <referenceBlock name="checkout.root">
                <arguments>
                    <argument name="jsLayout" xsi:type="array">
                        <item name="components" xsi:type="array">
                            <item name="checkout" xsi:type="array">                               
                                <item name="children" xsi:type="array">                           
                                    <item name="steps" xsi:type="array">
                                        <item name="children" xsi:type="array">
                                            <item name="shipping-step" xsi:type="array">
                                                <item name="children" xsi:type="array">
                                                    <item name="shippingAddress" xsi:type="array">
                                                        <item name="children" xsi:type="array">
                                                            <item name="shipping-address-fieldset" xsi:type="array"> 
                                                                <item name="children" xsi:type="array">                          
                                                                    <item name="postcode" xsi:type="array">
                                                                        <item name="validation" xsi:type="array">
                                                                            <item name="validate-digits" xsi:type="string">true</item>
                                                                            <item name="rspostcode" xsi:type="string">true</item>     
                                                                        </item>
                                                                    </item>               
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>                                            
                                        </item>
                                    </item>                                    
                                </item>
                            </item>                            
                        </item>
                    </argument>
                </arguments>
            </referenceBlock>
        </referenceContainer>
    </body>
</page>

replace script in onepage.phtml from app/design/frontend/Vendorname/Themename/Magento_Checkout/templates/onepage.phtml

replace script

<script type="text/x-magento-init">
{            
    "#checkout": {
        "Magento_Ui/js/core/app": <?php /* @escapeNotVerified */ echo $block->getJsLayout();?>
    }
}
</script>

to

<script type="text/x-magento-init">
    {
        "#checkout": {
            "Magento_Ui/js/core/app": <?php /* @escapeNotVerified */ echo $block->getJsLayout();?>
        },
        "*": {
            "RS_Validation/js/rsvalidation" : {}
        }
    }
</script>

Enjoy !!!

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.