emailAddress validator

Validate an email address

Options

Using with form field
The HTML attributes are used to set the validator options via the Declarative plugin
NameHTML attributeTypeDescription
messagedata-fv-email-address___messageStringThe error message
multipledata-fv-email-address___multipleStringAllow multiple email addresses, separated by a comma or semicolon. The default value is false
requireGlobalDomaindata-fv-email-address___require-global-domainBooleanRequire a global domain. The option is false by default.
a@b will be an invalid email address if you set requireGlobalDomain to true.
separatordata-fv-email-address___separatorStringRegex for character or characters expected as separator between addresses. By default, it is /[,;]/, i.e. comma or semicolon
This validator passes an empty field since the field might be optional. If the field is required, then use the notEmpty validator
You also can use the remote validator to connect and validate the email address on the server. For example, the Mailgun plugin demonstrates how to do this.
Using the ES6 module
// You might need to change the importing path
import { emailAddress } from '/vendors/@form-validation/cjs/validator-email-address';
const result = emailAddress().validate({
value: ...,
options: {
message: ...,
multiple: ...,
separator: ...,
},
});
/*
result is an object of
{
valid: true or false,
message: The error message
}
*/
Using the npm package
  • Install the validator package:
$ npm install @form-validation/validator-email-address
  • Use the emailAddress validator:
import { emailAddress } from '@form-validation/validator-email-address';
const result = emailAddress().validate({
value: ...,
options: {
message: ...,
multiple: ...,
separator: ...,
},
});

Basic example

Basic example

HTML5 Example

When the Declarative plugin is used, the emailAddress validator will be enabled automatically when using HTML5 type="email" attribute.
HTML5 example

NPM package example

The following snippet shows how to use the emailAddress validator with the npm package:
import { emailAddress } from '@form-validation/validator-email-address';
const res1 = emailAddress().validate({
value: 'niceandsimple@example.com',
options: {
message: 'The value is not a valid email address',
},
});
// res1.valid === true
const res2 = emailAddress().validate({
value: 'just"not"right@example.com',
options: {
message: 'The value is not a valid email address',
},
});
// res2.valid === false

See also

Changelog

v2.1.0
  • The validator doesn't work properly if the message property isn't defined
v2.0.0
  • Add the npm package
  • Add new requireGlobalDomain option
v1.5.0
  • Fix a bug that emailAddress validator doesn't support multiple email addresses on IE 11