validate method

  1. @override
String? validate(
  1. String? value, {
  2. BuildContext? context,
})
override

Validates the given value. Returns an error message String if validation fails, otherwise returns `null].

The context can optionally be used by rules needing access to localization, etc. This context is provided by the FieldValidator when getValidator is called.

Implementation

@override
String? validate(String? value, {BuildContext? context}) {
  if ((value ?? '').trim().isEmpty)
    return null; // Don't validate empty strings

  final upperCaseRegex = RegExp(r'[A-Z]');
  final lowerCaseRegex = RegExp(r'[a-z]');
  final digitRegex = RegExp(r'[0-9]');
  final specialCharRegex =
      RegExp(r'[!@#\$&*~-]'); // Add/remove characters as needed
  if (!upperCaseRegex.hasMatch(value!) ||
      !lowerCaseRegex.hasMatch(value) ||
      !digitRegex.hasMatch(value) ||
      !specialCharRegex.hasMatch(value)) {
    return message;
  }
  return null;
}