validateEmail static method

String? validateEmail(
  1. String? value
)

Validates the email input.

  • value: The email to be validated.

Returns a string error message if the email is invalid, otherwise null.

Implementation

static String? validateEmail(String? value) {
  if (value == null || value.isEmpty) {
    return 'Email is required.'; // Return error message if email is empty.
  }

  // Regular expression for email validation
  final emailRegExp = RegExp(r'^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$');

  if (!emailRegExp.hasMatch(value)) {
    return 'Invalid email address.'; // Return error message if email doesn't match the pattern.
  }

  return null; // Return null if email is valid.
}