checkPasswordStrength method
Check password strength
password
is the password to check
Returns a map with strength score and feedback
Implementation
Map<String, dynamic> checkPasswordStrength(String password) {
int score = 0;
final feedback = <String>[];
if (password.length >= 8) score += 1;
else feedback.add('Password should be at least 8 characters long');
if (password.contains(RegExp(r'[A-Z]'))) score += 1;
else feedback.add('Password should contain at least one uppercase letter');
if (password.contains(RegExp(r'[a-z]'))) score += 1;
else feedback.add('Password should contain at least one lowercase letter');
if (password.contains(RegExp(r'[0-9]'))) score += 1;
else feedback.add('Password should contain at least one number');
if (password.contains(RegExp(r'[!@#\$%^&*()_+\-=\[\]{}|;:,.<>?]'))) score += 1;
else feedback.add('Password should contain at least one symbol');
if (password.length >= 12) score += 1;
if (password.length >= 16) score += 1;
String strength;
if (score <= 2) strength = 'Weak';
else if (score <= 4) strength = 'Fair';
else if (score <= 6) strength = 'Good';
else strength = 'Strong';
return {
'score': score,
'strength': strength,
'feedback': feedback,
'maxScore': 7,
};
}