Created
May 1, 2013 12:24
-
-
Save openrijal/5495020 to your computer and use it in GitHub Desktop.
Java Class for verifying minimum password standard.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PasswordValidator { | |
// For character determinations | |
private static final int CHAR_LOWER_A = 'a'; | |
private static final int CHAR_LOWER_Z = 'z'; | |
private static final int CHAR_UPPER_A = 'A'; | |
private static final int CHAR_UPPER_Z = 'Z'; | |
private static final int CHAR_NUMERIC_ZERO = '0'; | |
private static final int CHAR_NUMERIC_NINE = '9'; | |
// Since the alpha and numeric checks handle the [a-zA-Z0-9] case in | |
// earlier if statement checks, we can then assume the surrounding characters | |
// within the range of the special char lower and upper values are in fact | |
// special characters. If it extends past the range, then it's no longer a symbol. | |
private static final int CHAR_LOWER_SPECIAL_CHAR = ' '; | |
private static final int CHAR_UPPER_SPECIAL_CHAR = '~'; | |
// Configurable variables | |
private static int minPasswordLength = 8; | |
private static int maxPasswordLength = 255; | |
private static int minLowerAlphaChars = 1; | |
private static int minUpperAlphaChars = 1; | |
private static int minSpecialChars = 1; | |
private static int minNumericalChars = 1; | |
// Override the default values | |
public static synchronized void configure(int newMinPasswordLength, int newMaxPasswordLength, | |
int newMinLowerAlphaChars, int newMinUpperAlphaChars, int newMinSpecialChars, | |
int newMinNumericalChars) { | |
minPasswordLength = newMinPasswordLength; | |
maxPasswordLength = newMaxPasswordLength; | |
minLowerAlphaChars = newMinLowerAlphaChars; | |
minUpperAlphaChars = newMinUpperAlphaChars; | |
minSpecialChars = newMinSpecialChars; | |
minNumericalChars = newMinNumericalChars; | |
} | |
public static String validatePassword(String password){ | |
int alphaLowerCharsCount = 0; | |
int alphaUpperCharsCount = 0; | |
int numericCharsCount = 0; | |
int specialCharsCount = 0; | |
String message = ""; // the error message | |
if(password == null){ | |
message = message+"Password cannot be null. It should follow the guidelines.\n\n"; | |
} | |
if (password.length() < minPasswordLength) { | |
message = message+"The password must be at least " + minPasswordLength + " characters in length.\n\n"; | |
} | |
if (password.length() > maxPasswordLength) { | |
message = message+"The password must be at less than " + maxPasswordLength + " characters in length.\n\n"; | |
} | |
// Count the characters | |
char passwordChar; | |
for (int i = 0; i < password.length(); i++) { | |
passwordChar = password.charAt(i); | |
// check for lower character | |
if (passwordChar >= CHAR_LOWER_A && passwordChar <= CHAR_LOWER_Z) { | |
alphaLowerCharsCount++; | |
} | |
// check for upper character | |
else if (passwordChar >= CHAR_UPPER_A && passwordChar <= CHAR_UPPER_Z) { | |
alphaUpperCharsCount++; | |
} | |
// check for special character | |
else if (passwordChar >= CHAR_NUMERIC_ZERO && passwordChar <= CHAR_NUMERIC_NINE) { | |
numericCharsCount++; | |
} | |
else if (passwordChar >= CHAR_LOWER_SPECIAL_CHAR && passwordChar <= CHAR_UPPER_SPECIAL_CHAR) { | |
specialCharsCount++; | |
} | |
else { | |
message = message+"Invalid password character entered. You can only use: a-z, A-Z, 0-9, Symbols [such as: !, @, #]\n\n"; | |
} | |
} | |
if (alphaLowerCharsCount < minLowerAlphaChars) { | |
message = message+"The password must contain at least " + minLowerAlphaChars + " lowercase alphabets (a-z) characters.\n\n"; | |
} | |
if (alphaUpperCharsCount < minUpperAlphaChars) { | |
message = message+"The password must contain at least " + minUpperAlphaChars + " uppercase alphabets (A-Z) characters.\n\n"; | |
} | |
if (numericCharsCount < minNumericalChars) { | |
message = message+"The password must contain at least " + minNumericalChars + " numerical (0-9) characters.\n\n"; | |
} | |
if (specialCharsCount < minSpecialChars) { | |
message = message+"The password must contain at least " + minSpecialChars + " special (symbols such as: !, @, #) characters.\n\n"; | |
} | |
return message; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment