isStringValidPassword function
bool
isStringValidPassword(
- String password, {
- PasswordComplexity complexity = PasswordComplexity.eightChar1Letter1Number,
Validates a password against the specified complexity requirements.
password The password string to validate.
complexity The complexity preset to validate against.
Defaults to PasswordComplexity.eightChar1Letter1Number.
Returns true if the password meets the complexity requirements.
Implementation
bool isStringValidPassword(
String password, {
PasswordComplexity complexity = PasswordComplexity.eightChar1Letter1Number,
}) {
switch (complexity) {
case PasswordComplexity.sixCharAnything:
// Minimum 6 characters, any characters allowed
return password.length >= 6;
case PasswordComplexity.eightChar1Letter1Number:
// Minimum 8 characters, at least one letter and one number
final regex = RegExp(r'^(?=.*[a-zA-Z])(?=.*\d).{8,}$');
return regex.hasMatch(password);
case PasswordComplexity.eight2Upper2Number2Special:
// Minimum 8 characters, at least 2 uppercase, 2 numbers, 2 special chars
if (password.length < 8) return false;
final uppercaseCount = RegExp(r'[A-Z]').allMatches(password).length;
final numberCount = RegExp(r'\d').allMatches(password).length;
final specialCount =
RegExp(r'[!@#$%^&*()_+\-=\[\]{};:"\\|,.<>\/?]').allMatches(password).length;
return uppercaseCount >= 2 && numberCount >= 2 && specialCount >= 2;
}
}