---
title: "Magento 2: How to Add Custom Validation To System Configuration Field"
url: "https://magecomp.com/blog/magento-2-how-to-add-custom-validation-to-system-configuration-field/"
date: "2020-08-21T17:47:11+00:00"
modified: "2025-02-20T09:18:14+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 482
reading_time: "3 min read"
summary: "Hello Magento Friends ?,"
description: "Many times you need to set custom validation on configuration fields as per your requirement. Find out how you can achieve it programmatically in Magento 2."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Top 5 Best CDN Providers for Magento 2 "
    url: "https://magecomp.com/blog/cdn-providers-magento-2/"
  - title: "How to Define Custom Form Validation in Magento"
    url: "https://magecomp.com/blog/define-custom-form-validation-in-magento/"
  - title: "How to Get CMS Page List in System Configuration in Magento 2?"
    url: "https://magecomp.com/blog/cms-page-list-in-system-configuration-in-magento-2/"
---

# Magento 2: How to Add Custom Validation To System Configuration Field

_Published: August 21, 2020_  
_Author: Dhiren Vasoya_  

![How to Add Custom Validation To System Configuration Field in magneto 2](https://magecomp.com/blog/wp-content/uploads/2020/08/How-to-Add-Custom-Validation-To-System-Configuration-Field-in-magneto-2-1024x512.webp)

**Hello Magento Friends ?,**

What’s going on? Welcome to Magento 2 technical solutions by MageComp. Today I am here demonstrating How to Add Custom Validation To System Configuration Field in Magento 2. Earlier I explained about [**How to Remove Apply Discount Code From Checkout Payment Page in Magento 2**](https://magecomp.com/blog/how-to-remove-apply-discount-code-from-checkout-payment-page-in-magento-2/).
[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-3.webp)](https://magecomp.com/magento-malware-removal.html)

## Beginning:
Validation is to restrict data entry on specific fields to avoid false entry. If the data does not match the validation rules it provides an error message telling the user to enter the correct data and do not allow the user to proceed further.
By default, Magento 2 provides some default built-in classes to validate system configuration fields. E.g. required field, validate non-negative numbers, a number greater than zero.
But what if you want to validate your system configuration field based on your requirement? E.g. validate the mobile number field with a minimum 10 digit number.
No worries, with the help of the below code you can Add Custom Validation in Magento 2 System Configuration Field.
So let us find out ?
## Steps to Add Custom Validation To System Configuration Field in Magento 2:
**Step 1:** Update your **system.xml** file inside the following path:
**app\code\Vendor\Extension\etc\adminhtml\**

Now, update the code with the below code:
```
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Backend/etc/system_file.xsd">
    <system>
                    <label>Custom Validation</label>
            <resource>Vendor_Extension::custom_config</resource>
            <group id="general" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <field id="mobilenumber" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Mobile Number</label>
                    <validate>required-entry validate-ten-degit</validate>
                </field>
            </group>
            </system>
</config>
```

**Note: Here “validate-ten-digit” is a Custom validation class.**

[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-1.webp)](https://magecomp.com/magento-performance-optimization.html)

**Step 2:** Next, create a **requirejs-config.js** file inside the below path:
**app\code\Vendor\Extension\view\adminhtml\**

And add this code:
```
var config = {
    config: {
        mixins: {
            'mage/validation': {
                'Vendor_Extension/js/admin-config/validator-rules-mixin': true
            }
        }
    }
};
```

**Step 3:** Next, create a **validator-rules-mixin.js** file inside the below path:
**app\code\Vendor\Extension\view\adminhtml\web\js\admin-config\**

And add this code:
```
define([
 'jquery'
], function ($) {
    'use strict';
    return function (target) {
        $.validator.addMethod(
            'validate-ten-digit',
            function (value) {
                return !(value.length < 10);
            },
            $.mage.__('Please enter a 10 digit number.')
        );
        return target;
    };
});
```

**Step 4:** Finally run below commands.
```
php bin/magento setup:upgrade
 php bin/magento setup:static-content:deploy -f
 php bin/magento cache:flush
```

That’s it.
## Wrap Up:
I hope now you found the way to Add Custom Validation to System Configuration Field in Magento 2. If you find it hard to get, mention right away in the comment section below. I will be glad to fix up. Share the article with your friends and stay in touch with us.
[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-3.webp)](https://magecomp.com/services/hyva-theme-development/)

**Happy Coding ?**


---

_View the original post at: [https://magecomp.com/blog/magento-2-how-to-add-custom-validation-to-system-configuration-field/](https://magecomp.com/blog/magento-2-how-to-add-custom-validation-to-system-configuration-field/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-11-07 14:42:12 UTC_  
