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:
Format | Example |
---|
Single email address | a@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 cases | a@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) {
const value = element.value;
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