When you parse String data, you must remove the spaces on the right and left. This is done using the Strimg#trim function as follows:
password = password.trim();
To parse each character of a string, you can convert it to a char array, so it will be easier for you to fulfill your requirements:
char[] arrPassword = password.toCharArray();
Now you can evaluate char using the following functions: Character#isUpperCase , Character#isLowerCase , Character#isDigit .
And last but not least, you may have a line with special characters that you need to check and check if the actual character is inside this line. This can be achieved with String#indexOf and String#valueOf , this is las for converting char to String type.
Here is a sample code for all of this explanation:
public static final String SPECIAL_CHARACTERS = " !@ #$%^&*()~`-=_+[]{}|:\";',./<>?"; public static final int MIN_PASSWORD_LENGTH = 8; public static final int MAX_PASSWORD_LENGTH = 20; public static boolean isAcceptablePassword(String password) { if (TextUtils.isEmpty(password)) { System.out.println("empty string."); return false; } password = password.trim(); int len = password.length(); if(len < MIN_PASSWORD_LENGTH || len > MAX_PASSWORD_LENGTH) { System.out.println("wrong size, it must have at least 8 characters and less than 20."); return false; } char[] aC = password.toCharArray(); for(char c : aC) { if (Character.isUpperCase(c)) { System.out.println(c + " is uppercase."); } else if (Character.isLowerCase(c)) { System.out.println(c + " is lowercase."); } else if (Character.isDigit(c)) { System.out.println(c + " is digit."); } else if (SPECIAL_CHARACTERS.indexOf(String.valueOf(c)) >= 0) { System.out.println(c + " is valid symbol."); } else { System.out.println(c + " is an invalid character in the password."); return false; } } return true; }
Sentence System.out.println(c + " is an invalid character in the password."); - it's just checking the result of the analysis of the actual character.
Luiggi Mendoza
source share