emailValidator top-level property

String? Function(String?)? emailValidator
getter/setter pair

Validates an email input field.

Returns an error message if:

  • The input is empty or null.
  • The input does not match a valid email pattern.

Returns null if the input is valid.

Implementation

String? Function(String?)? emailValidator = (String? value) {
  if (value == null || value.isEmpty) {
    return 'Please enter an email';
  }

  final emailRegex = RegExp(
    r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$",
  );

  if (!emailRegex.hasMatch(value)) {
    return 'Please enter a valid email';
  }
  return null;
};