Created
November 11, 2021 08:28
-
-
Save rafsanahmad/0c6c29f25b406f28161432ff7d85a261 to your computer and use it in GitHub Desktop.
A utils for testing Password rule.
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
import java.util.regex.Pattern; | |
public class PasswordUtil { | |
private static String MsgLen8 = "String Must be 8 characters long"; | |
private static String MsgUpper = "Must contain at least one upper case letter"; | |
private static String MsgLower = "Must contain at least one lower case letter"; | |
private static String MsgDigit = "Must contain at least one digit"; | |
private static String MsgSp = "Must contain at least one special character from this list: !@#$%^&*()+-.,\"'(){}[]:;?/\\"; | |
private static Pattern pattern = Pattern.compile("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()+=])(?=\\\\S+$).{4,}$"); | |
private static Pattern UpperPt = Pattern.compile("(?=.*[A-Z])"); | |
private static Pattern LowerPt = Pattern.compile("(?=.*[a-z])"); | |
private static Pattern DigitPt = Pattern.compile("(?=.*\\d)"); | |
private static Pattern specialPt = Pattern.compile("[!@#$%&*()_+=|<>?{}\\[\\]~-]"); | |
static String msg = "Password must be at least 8 characters long. It must contain at least one upper-case letter, one lower-case letter, one number and one special character."; | |
public static String IsStrong(String Password) { | |
if (Password.length() < 8) { | |
return msg; | |
} else if (!UpperPt.matcher(Password).find()) { | |
return msg; | |
} else if (!LowerPt.matcher(Password).find()) { | |
return msg; | |
} else if (!DigitPt.matcher(Password).find()) { | |
return msg; | |
} else if (!specialPt.matcher(Password).find()) { | |
return msg; | |
} else { | |
return ""; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment