Magento

Magento 2 Add Custom Validation Checkout

Written by Mahesh Makwana

Apr 01

Magento 2 Add Custom Validation Checkout

We learn how to add custom validation checkout in Magento 2

Sometimes we need to use custom validation on checkout page. Magento provide many default validation. Here given some steps to create custom validation into checkout Magento 2:

  • Step-1: Create requirejs-config.js at app/code/Dolphin/CustomValidation/view/frontend
var config = {
    config: {
        mixins: {
            'Magento_Ui/js/lib/validation/validator': {
                'Dolphin_CustomValidation/js/test-mixin': true
            }
        }
    }
}

Magento 2 Fast checkout extension

  • Step-2: Create test-mixin.js at app/code/Dolphin/CustomValidation/view/frontend/web/js
define(
    ['jquery'],
   function($) {
  "use strict";
    return function(validator) {
        validator.addRule(
            'validate-five-words',
                function(value, element) {
                    // add you logic here
                    //return value.split(' ').length == 5;
                },
            $.mage.__('If any Error then put here the Message')
        );
        return validator;
    }
});
  • Step-3: You can used this validation using layout checkout_index_index.xml.
<item name="validation" xsi:type="array">
    <item name="required-entry" xsi:type="boolean">true</item>
    <item name="validate-five-words" xsi:type="boolean">true</item>
</item>

Now you can check your checkout page form where you put above validation field. Now successfully apply our custom validation.

I hope this helps you. For any doubts regarding this topic, please write your doubts in the comments section.

Written by Mahesh Makwana

Our Magento Store

Do not miss a chance to grab exciting offers on Magento Development at Dolphin Web Solution. We are providing discounts on various Magento services this season. Go and grab yours today at our Magento Store.

Multiple Wishlist for Magento 2

Multiple Wishlist for Magento 2

₹ 3106

Wallet and Reward Points for Magento 2

Wallet and Reward Points for Magento 2

₹ 9476

RMA for Magento 2

RMA for Magento 2

₹ 11865

₹ 14254
Abandoned Cart Email for Magento 2

Abandoned Cart Email for Magento 2

₹ 6291

Simple Details on Configurable Product for Magento 2

Simple Details on Configurable Product for Magento 2

₹ 7883

₹ 9476
Frequently Bought Together for Magento 2

Frequently Bought Together for Magento 2

₹ 5494

₹ 7087

Let's Start Your Project

Get free consultation for your digital product idea to turn it into reality!

Copyright © 2023 DOLPHIN WEB SOLUTION. All rights reserved.

TO TOP