Validate a field array

Hello,

is there a possibility to apply a validation to multiple fields?
In my example there are several email fields. But since I don't want to store the same validation for each field, I thought to solve the whole thing using an array.
Is there such a possibility?

Here is my code. I know that emailArray.addValidator don´t work.

var emailArray = ['E_x002d_mail_x0020__x0028_invoic', 'E_x002d_Mail_x0020_Dunning_x0020', 'CP_x003a__x0020_E_x002d_Mail_x00', 'CP_x003a__x0020_E_x002d_mail_x000'];
emailArray.addValidator({
name: 'Check Email',
error: 'Enter a correct E-Mail',
validate:function(value){
var Reg = new RegExp(/([\w\.\-_]+)?\w+@[\w-_]+(\.\w+){1,}/);
    for(i=0; i< emailArray.length; i++){
        if(Reg.test(emailArray[i])){
            return true
        }
            return false;
        }
    }
});

I hope you can help me fixing my problem.

Dear @Sternchen,
You can store validator as an object, and add it to multiple fields. Also, don't return true on each successful check, or it will return true as long as the first value matches.

Try it like this:

var emailArray = ['E_x002d_mail_x0020__x0028_invoic', 'E_x002d_Mail_x0020_Dunning_x0020', 'CP_x003a__x0020_E_x002d_Mail_x00', 'CP_x003a__x0020_E_x002d_mail_x000'];
var emailValidator = {
  name: 'Check Email',
  error: 'Enter a correct E-Mail',
  validate:function(value){
    var Reg = new RegExp(/([\w\.\-_]+)?\w+@[\w-_]+(\.\w+){1,}/);
    if(Reg.test(value){
        return true
    }
    return false;
  }
}
for(i=0; i< emailArray.length; i++){
  fd.field(emailArray[i]).addValidator(emailValidator);
}

It worked thank you :slight_smile: