i’m using Magento 2.4.1 and i add custom validation with a custom error message to detect which character is wrong and add it to the message:
define([
'jquery'
], function ($) {
return function () {
var validator = this;
$.validator.addMethod(
'validate-unwanted-character',
function (value,elm) {
if (value.length != 0){
var regExp = /[a-zA-Z0-9u3040-u30ffu3400-u4dbfu4e00-u9fffuf900-ufaffuff66-uff9f]/g;
if(!regExp.test(value) && value.length < 2){
var errorCharacter = value.replace(regExp, '');
validator.errChar = $.mage.__('"%1" is an invalid character's, please correct your input.').replace('%1', errorCharacter)
return false;
}else if(!value.match("^[-A-Za-z0-9()./ u3040-u30ffu3400-u4dbfu4e00-u9fffuf900-ufaffuff66-uff9f]+$")){
var errorCharacter = value.replace(regExp, '');
validator.errChar = $.mage.__('"%1" is an invalid character's, please correct your input.').replace('%1', errorCharacter)
return false;
}
}
return true;
},
validator.errChar //this should contain the error message but always empty
);
}
});
i add this validation for firstname,lastname,company, and street in edit address form in frontend. However the error message is always empty
when i do alert(validator.errChar)
inside the if
statement it is showing correct message , i pass the message to this variable so i can have dynamic message for each input element