regex password validation flutter code example
Example 1: Password validation in dart with RegExp
bool isPasswordCompliant(String password, [int minLength = 8]) {
if (password == null || password.length < minLength) {
return false;
}
bool hasUppercase = password.contains(RegExp(r'[A-Z]'));
if (hasUppercase) {
bool hasDigits = password.contains(RegExp(r'[0-9]'));
if (hasDigits) {
bool hasLowercase = password.contains(RegExp(r'[a-z]'));
if (hasLowercase) {
bool hasSpecialCharacters = password.contains(RegExp(r'[!@#$%^&*(),.?":{}|<>]'));
return hasSpecialCharacters;
}
}
}
return false;
}
Example 2: flutter regex validation
You could make the first part optional matching either a + or 0 followed by a 9. Then match 10 digits:
^(?:[+0]9)?[0-9]{10}$
^ Start of string
(?:[+0]9)? Optionally match a + or 0 followed by 9
[0-9]{10} Match 10 digits
$ End of string
Regex demo