Transform the email address validator

The emailAddress validator is able to check the validity of multiple email addresses in a single field at the same time.
By using the multiple option, the multipleEmails field in the sample code below can consist of different email address:
FormValidation.formValidation(document.getElementById('demoForm'), {
fields: {
multipleEmails: {
validators: {
emailAddress: {
multiple: true,
message: 'The input is not valid',
},
},
},
},
});
By default, the email addresses must be separated by commas (,). You can change this value via the separator option.
Let's say that you don't want to strict the format of multiple email addresses. Assume that the following formats are accepted:
FormatExample
Single email addressa@b.com
There is no space around ,a@b.com,c@d.com
There is one space around ,a@b.com, c@d.com
Mixed casesa@b.com , c@d.com, e@f.com
We can use the Transformer plugin to transform a value into the normalized one. We simply remove all spaces from the input:
FormValidation.formValidation(document.getElementById('demoForm'), {
fields: {
// ...
},
plugins: {
transformer: new FormValidation.plugins.Transformer({
multipleEmails: {
emailAddress: function (field, element, validator) {
// Get the field value
const value = element.value;
// We remove all spaces from the input
return value.replace(/\s/g, '');
},
},
}),
},
});
The sample code above transforms the value of the multipleEmails field when being validated against the emailAddress validator.
Transform the email address validator

See also