validEmail static method
Returns a validation function that checks if the input value is a valid email address.
The validation function will return an error message if the value does not match the email regex pattern,
otherwise it returns null
indicating the value is valid.
value
: The input value to validate.
errorMessage
: The error message to display if the validation fails. If not provided, a default message is used.
Implementation
static String? Function() validEmail(
String value, {
String? errorMessage,
}) {
return () {
final emailRegex = RegExp(
r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$',
);
if (!emailRegex.hasMatch(value)) {
return errorMessage ?? 'Please enter a valid email address.';
}
return null;
};
}