password static method

String? password(
  1. String? value, {
  2. int minLength = 8,
  3. bool requireSpecialChar = true,
  4. bool requireUppercase = true,
  5. bool requireLowercase = true,
  6. bool requireNumber = true,
})

Validates a strong password with optional rules.

Implementation

static String? password(
  String? value, {
  int minLength = 8,
  bool requireSpecialChar = true,
  bool requireUppercase = true,
  bool requireLowercase = true,
  bool requireNumber = true,
}) {
  if (value == null || value.isEmpty) return 'Password is required';

  if (value.length < minLength) {
    return 'Password must be at least $minLength characters';
  }

  if (requireUppercase && !RegExp(r'[A-Z]').hasMatch(value)) {
    return 'Password must contain at least one uppercase letter';
  }

  if (requireLowercase && !RegExp(r'[a-z]').hasMatch(value)) {
    return 'Password must contain at least one lowercase letter';
  }

  if (requireNumber && !RegExp(r'[0-9]').hasMatch(value)) {
    return 'Password must contain at least one number';
  }

  if (requireSpecialChar &&
      !RegExp(r'[!@#$%^&*(),.?":{}|<>]').hasMatch(value)) {
    return 'Password must contain at least one special character';
  }

  // Check for common weak patterns
  if (RegExp(
    r'^(123456|password|admin|qwerty|111111|abc123)',
  ).hasMatch(value.toLowerCase())) {
    return 'Password is too common, please choose a stronger one';
  }

  return null;
}