How can we implement a password strength checker in Android?
This seems to work:
//ASSERT password not null
private float getRating(String password) throws IllegalArgumentException {
if (password == null) {throw new IllegalArgumentException();}
int passwordStrength = 0;
if (password.length() > 5) {passwordStrength++;} // minimal pw length of 6
if (password.toLowerCase()!= password) {passwordStrength++;} // lower and upper case
if (password.length() > 8) {passwordStrength++;} // good pw length of 9+
int numDigits= Utilities.getNumberDigits(password);
if (numDigits > 0 && numDigits != password.length()) {passwordStrength++;} // contains digits and non-digits
return (float)passwordStrength;
}
Utilities class
public static int getNumberDigits(String inString){
if (isEmpty(inString)) {
return 0;
}
int numDigits= 0;
int length= inString.length();
for (int i = 0; i < length; i++) {
if (Character.isDigit(inString.charAt(i))) {
numDigits++;
}
}
return numDigits;
}
public static boolean isEmpty(String inString) {
return inString == null || inString.length() == 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With