email static method
Validator that requires the field's value pass an email validation test.
This validator uses Regex of HTML5 email validator.
RegExp(r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$");
Validate that the field matches a valid email pattern
TextFormField(
decoration: InputDecoration(
labelText: 'Email',
),
validator: Validators.email('Invalid email address'),
),
Implementation
static FormFieldValidator<String> email(String errorMessage) {
return (value) {
if (value == null) {
value = '';
}
if (value.isEmpty)
return null;
else {
final emailRegex = RegExp(
r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$");
if (emailRegex.hasMatch(value))
return null;
else
return errorMessage;
}
};
}