isPassword static method
bool
isPassword(
- String? password,
- {int minLength = 4,
- int? maxLength,
- bool shouldContainNumber = false,
- bool shouldContainSpecialChars = false,
- bool shouldContainCapitalLetter = false,
- bool shouldContainSmallLetter = false,
- Function? reason,
- void isNumberPresent( )?,
- void isSpecialCharsPresent( )?,
- void isCapitalLetterPresent( )?,
- void isSmallLetterPresent( )?,
- void isMaxLengthFailed(
- void isMinLengthFailed(
Todo: Implement reason for failure For Validated passwords strings
Implementation
static bool isPassword(
String? password, {
int minLength = 4,
int? maxLength,
bool shouldContainNumber = false,
bool shouldContainSpecialChars = false,
bool shouldContainCapitalLetter = false,
bool shouldContainSmallLetter = false,
Function? reason,
void Function(bool)? isNumberPresent,
void Function(bool)? isSpecialCharsPresent,
void Function(bool)? isCapitalLetterPresent,
void Function(bool)? isSmallLetterPresent,
void Function()? isMaxLengthFailed,
void Function()? isMinLengthFailed,
}) {
if (password == null) {
return false;
}
if (password.trim().length == 0) {
return false;
}
if (password.length < minLength) {
if (isMinLengthFailed != null) isMinLengthFailed();
return false;
}
if (maxLength != null) {
if (password.length > maxLength) {
if (isMaxLengthFailed != null) isMaxLengthFailed();
return false;
}
}
if (shouldContainNumber) {
final numberRegex = RegExp(r"[0-9]+");
if (!numberRegex.hasMatch(password)) {
if (isNumberPresent != null) isNumberPresent(false);
return false;
} else if (isNumberPresent != null) isNumberPresent(true);
}
if (shouldContainCapitalLetter) {
final capitalRegex = RegExp(r"[A-Z]+");
if (!capitalRegex.hasMatch(password)) {
if (isCapitalLetterPresent != null) isCapitalLetterPresent(false);
return false;
} else if (isCapitalLetterPresent != null) isCapitalLetterPresent(true);
}
if (shouldContainSmallLetter) {
final smallLetterRegex = RegExp(r"[a-z]+");
if (!smallLetterRegex.hasMatch(password)) {
if (isSmallLetterPresent != null) isSmallLetterPresent(false);
return false;
} else if (isSmallLetterPresent != null) isSmallLetterPresent(true);
}
if (shouldContainSpecialChars) {
// final numberRegex = RegExp(r'(?=.*?[#?!@$%^&*-])');
final specialRegex = RegExp(r"[\'^£$%!&*()}{@#~?><>,.|=_+¬-]");
if (!specialRegex.hasMatch(password)) {
if (isSpecialCharsPresent != null) isSpecialCharsPresent(false);
return false;
} else if (isSpecialCharsPresent != null) isSpecialCharsPresent(true);
}
return true;
}