0
magento2.3.3/pub/static/adminhtml/Magento/backend/da_DK/Magento_Ui/js/lib/validation/rules.js

I need to override rules.js for specific Danish locale only for Admin area, how can I achieve that.

In short, if I describe then I need to make a small change in validate-number to support 100.000,00

'validate-number': [
            function (value) {
                return utils.isEmptyNoTrim(value) ||
                    !isNaN(utils.parseNumber(value)) && /^\s*-?\d*(.\d*)*(\,\d*)?\s*$/.test(value);
            },

1 Answer 1

2

Create requirejs-config.js in Vendor/Module/view/base/.

var config = {
    config: {
        mixins: {
            'Magento_Ui/js/lib/validation/rules': {
                'Vendor_Module/js/lib/validation/rules': true
            }
        }
    }
};

Create rules.js in Vendor/Module/view/base/web/js/lib/validation.

define([
    'jquery',
    'underscore',
    'moment',
    'mage/translate'
], function ($, _, moment) {
    'use strict';

    return function (validator) {
        var validators = {
            'validate-number-custom': [
                function (value) {
                    return /^[a-zA-Z ]*$/i.test(value);
                },
                $.mage.__('Please enter a valid number in this field.')
            ]
        };

        validators = _.mapObject(validators, function (data) {
            return {
                handler: data[0],
                message: data[1]
            };
        });

        return $.extend(validator, validators);
    };
});

Add your changes in return line. And use validate-number-custom validation for your field.

And execute php bin/magento setup:upgrade and php bin/magento setup:static-content:deploy.

2
  • Can you let me know what to put in rules.js for changing default validate number rule
    – Arjun
    Dec 14, 2019 at 9:09
  • @Arjun See my updated answer. Dec 14, 2019 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.