I have a custom admin form and unfortunately it doesnt have a UI component xml file.

I was able to add the no-whitespace class validation.

I would add also a regexp pattern, but I don't know where I should specify the format.

I tried

'class' => 'no-whitespace pattern',
'pattern' => '^[0-9]+(,[0-9]+)*$'

but only no-whitespace works fine

any idea? tnx

1 Answer 1


Antonio Pedicini, instead of add custom pattern direct to input field ,my suggest to create a magento 2 create custom validation

So, extend rules.js to add custom validation rules for this pattern.

First create folder inside at app/design/adminhtml/Magento/backend/Magento_Ui/web/js/lib/validation/rules.js if its already exist then skip it keep file into below location from core module.

And that files add

 "MyCustompattern": [
            function(value) {
                return value.match(/^[0-9]+(,[0-9]+)*$/);
            $.mage.__('My Custom Validaion')


"no-whitespace": [
            function(value) {
                return /^\S+$/i.test(value);
            $.mage.__('No white space please')

Then add MyCustompattern in 'class' => 'no-whitespace MyCustompattern'

Final delete all file from var/view_preprocessed and var/cache,var/page_cache. Removepub/static `folder contents and run deploy command: for see ,it is working or not.

php bin/magento setup:static-content:deploy

Or,you can follow below blog for create custom rules



Adding custom global javascript validation

  • tnx Amit, but in that case the rule will be site wide? I just need it on 1 admin form, why do you think is not a good idea to use pattern on the input? Also, do you have any example how to use it? i'm struggling to find any
    – apedic
    Commented Sep 27, 2017 at 9:30
  • Amit, would that solution extend or override the core rule.js so I need to check against any update?
    – apedic
    Commented Sep 28, 2017 at 10:27

Your Answer

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

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