isPasswordValidator method
Validates whether the string meets password strength requirements.
This method allows customization for password strength, including:
- Minimum Length (
minLength
) → default is 6. - Uppercase Character Count (
uppercaseCharCount
) → default is 0 (not required). - Lowercase Character Count (
lowercaseCharCount
) → default is 0 (not required). - Numeric Character Count (
numericCharCount
) → default is 0 (not required). - Special Character Count (
specialCharCount
) → default is 0 (not required).
A password is valid if it meets all specified conditions.
Returns:
true
if the password satisfies all conditions.false
otherwise.
Example Usage:
String password1 = "Secure123!";
bool isValid1 = password1.isPasswordValidator(
minLength: 8,
uppercaseCharCount: 1,
lowercaseCharCount: 1,
numericCharCount: 1,
specialCharCount: 1,
); // true
String password2 = "abc123";
bool isValid2 = password2.isPasswordValidator(
minLength: 8,
uppercaseCharCount: 1,
); // false (too short, missing uppercase)
String password3 = "P@ssw0rd";
bool isValid3 = password3.isPasswordValidator(
minLength: 6,
specialCharCount: 1,
); // true
Edge Cases:
- Password must not contain spaces.
- Default minimum length is 6, but can be adjusted.
Implementation
bool isPasswordValidator(
{int minLength = 6,
int uppercaseCharCount = 0,
int lowercaseCharCount = 0,
int numericCharCount = 0,
int specialCharCount = 0}) {
if (isEmptyOrNull) return false;
if (this.contains(' ')) return false;
if (minLength != 0 &&
!Validator.hasMinimumLength(this, minLength == 0 ? 6 : minLength)) {
return false;
}
if (uppercaseCharCount != 0 &&
!Validator.hasMinimumUppercase(this, uppercaseCharCount)) {
return false;
}
if (lowercaseCharCount != 0 &&
!Validator.hasMinimumLowercase(this, lowercaseCharCount)) {
return false;
}
if (numericCharCount != 0 &&
!Validator.hasMinimumNumericCharacters(this, numericCharCount)) {
return false;
}
if (specialCharCount != 0 &&
!Validator.hasMinimumSpecialCharacters(this, specialCharCount)) {
return false;
}
return true;
}