How to Set Validation for Password In TextFormField ?

Password Validator

Password Validator Code


validator: (password) {
  // Ensure password is not null
  password = password ?? '';

  // Validate password length
  if (password.length < 8) {
    return AppErrors.passwordLengthErrorText;
  }

  // Check if password contains at least one numeric character
  if (!RegExp(r'[0-9]').hasMatch(password)) {
    return AppErrors.passwordNumericErrorText;
  }

  // Check if password contains both uppercase and lowercase letters
  if (!RegExp(r'[A-Z]').hasMatch(password) || !RegExp(r'[a-z]').hasMatch(password)) {
    return AppErrors.passwordAlphabetErrorText;
  }

  // If all checks pass, return null
  return null;
},

  

Comments

Popular Posts