cancel
Showing results for 
Search instead for 
Did you mean: 

i need your help !

i need your help !

I am trying to do a custom js validation for a form field, I added this in my phtml file :

<script type="text/javascript">

//<![CDATA[
var theForm = new VarienForm('form-validate', true);
Validation.add('validate-testx','You failed to enter baz!',function(v){

if(v == 'baz')
{
return true;
}
return false;
});
//]]>
</script>

 

and added "validate-testx" to the field's css class but it did not work. I am using magento 2, please help.

1 REPLY 1

Re: i need your help !

Try to add this code :

 

<script type="text/javascript">
require([
'jquery', // jquery Library
'jquery/ui', // Jquery UI Library
'jquery/validate', // Jquery Validation Library
'mage/translate' // Magento text translate (Validation message translte as per language)
], function($){
$.validator.addMethod(
'validate-testx','You failed to enter baz!',function(value) {

return $.mage.__('Valid')); // Validation logic here
}, $.mage.__('Not a valid'));

});
</script>

Hope it helps !

 

if issue solved, Click Kudos & Accept as Solution