Adding custom global javascript validation

by lord_of_strings   Last Updated June 18, 2016 08:03 AM

I have done some digging in internet but I haven't found any confirmed solution. What is the way to add custom js validation rule, that can be used globally as for example frontend_class for attributes or validation classes for form's inputs? It would be the best if I could store it in one of my modules.

By the way, for version 2.05 is there any class validating by min or max lenght of string/number of characters? The ones I have found in net don't work (seems to me they are outdated).



Answers 1


Assuming you add a validation class via the frontend_class let's say "validate-custom-class" you'll need to use the following JS to add a custom validation based on a this class:

require([
'jquery',
'jquery/ui',
'jquery/validate',
'mage/translate'
], function($){ 
$.validator.addMethod(
'validate-custom-class', function (value) { 
    // Add your validation logic here
    // Needs to return true if validation pass
    // Or false if validation fails
}, $.mage.__('Field is not valid'));
});
Raphael at Digital Pianism
Raphael at Digital Pianism
June 18, 2016 12:06 PM

Related Questions



Translate javascript validation messages

Updated October 02, 2018 09:09 AM

How to add addition script to existing validation class

Updated September 19, 2018 14:09 PM