How To

Magento 2: How to Add Custom Validation Rule in knockout Validation Rules

Hello Magento Friends,

Welcome back to Magento “How To” blog category. Today we will discuss Magento 2: How to Add Custom Validation Rule in knockout Validation Rules. In case you haven’t taken a glance at our previously published blog, How to Add Customer Name in CMS Page/Block in Magento 2.

Introduction:

At the time of development, there is plenty of data received from customers. It is required to apply validation to these data. And developers have requirements to add a custom Validation on the front side while submitting any data to the server. So let us look at the steps, Magento 2: How to Add Custom Validation Rule in knockout Validation Rules. 

Steps to Add Custom Validation Rule in knockout Validation Rules in Magento 2:

Step 1: Create requirejs-config.js at the below path 

app\code\Vendor\Extension\view\frontend\requirejs-config.js

And the below code

var config = {
   config: {
       mixins: {
           'Magento_Ui/js/lib/validation/validator': {
                 'Vendor_Extension/js/custom-validation': true
        },
     }
  }
};

Using Mixins, we override the Validator file to our file. 

Step 2: Now, create a custom-validation.js file at the below path

app\code\Vendor\Extension\view\frontend\web\js\custom-validation.js

And add the code as follows

define(['jquery'],
   function($) {
  "use strict";
return function(validator) {
validator.addRule(
'validate-name',
     function(value) { /* Custom validation logic */},
        $.mage.__('If any Error then put here the Message')
    );
   return validator;
 }
});

We have to override the AddRule function for adding our custom rules. So from now if you put ‘validate-name’ anywhere in the frontend input field it will validate the field as per custom logic.

Conclusion:

Therefore, all are now able to Add Custom Validation Rule in knockout Validation Rules in Magento 2. If you come across any trouble, mention it in the comment section below without any hesitation. Spread the solution further with your Magento colleagues. Stay updated with us for the latest solution.

Happy Coding!

Click to rate this post!
[Total: 5 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 day ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 day ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

2 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

5 days ago