---
title: "Magento 2: How to Add Custom Validation Rule in knockout Validation Rules"
url: "https://magecomp.com/blog/magento-2-add-custom-validation-rule-in-knockout-validation-rules/"
date: "2021-01-25T17:19:47+00:00"
modified: "2025-02-20T10:25:04+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 307
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "Sometimes developers need validation in the data. Learn Magento 2: How to Add Custom Validation Rule in knockout Validation Rules."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Lock Customer Account if They belong to Specific Customer Group in Magento 2"
    url: "https://magecomp.com/blog/lock-customer-account-of-specific-magento-2-customer-group/"
  - title: "How to Set Up Cron Jobs in Magento 2"
    url: "https://magecomp.com/blog/set-up-cron-jobs-in-magento-2/"
  - title: "Top 10 Best Magento 2 Banner Slider Extensions"
    url: "https://magecomp.com/blog/top-magento-2-banner-slider-extensions/"
---

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

_Published: January 25, 2021_  
_Author: Dhiren Vasoya_  

![How to Add Custom Validation Rule in knockout Validation Rules in M2](https://magecomp.com/blog/wp-content/uploads/2021/01/How-to-Add-Custom-Validation-Rule-in-knockout-Validation-Rules-in-M2-1024x512.webp)

**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**](https://magecomp.com/blog/add-customer-name-in-cms-page-block-in-magento-2/).
[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-4.webp)](https://magecomp.com/services/hire-magento-programmer/)

## 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;
 }
});
```

[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-4.webp)](https://magecomp.com/magento-malware-removal.html)

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.
[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2023/10/Magento-2-Speed-Performance-Optimization-Services-CTA-4.webp)](https://magecomp.com/magento-performance-optimization.html)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/magento-2-add-custom-validation-rule-in-knockout-validation-rules/](https://magecomp.com/blog/magento-2-add-custom-validation-rule-in-knockout-validation-rules/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-10-19 06:37:18 UTC_  
