validatePassword static method
Validates the password input.
value
: The password to be validated.
Returns a string error message if the password is invalid, otherwise null.
Implementation
static String? validatePassword(String? value) {
if (value == null || value.isEmpty) {
return 'Password is required.'; // Return error message if password is empty.
}
// Check for minimum password length
if (value.length < 6) {
return 'Password must be at least 6 characters long.'; // Return error message for short passwords.
}
// Check for uppercase letters
if (!value.contains(RegExp(r'[A-Z]'))) {
return 'Password must contain at least one uppercase letter.'; // Return error if no uppercase letter is found.
}
// Check for numbers
if (!value.contains(RegExp(r'[0-9]'))) {
return 'Password must contain at least one number.'; // Return error if no number is found.
}
// Check for special characters
if (!value.contains(RegExp(r'[!@#$%^&*(),.?":{}|<>]'))) {
return 'Password must contain at least one special character.'; // Return error if no special character is found.
}
return null; // Return null if password is valid.
}