Validate a password in Java
This post covers various methods to validate a password in Java.
We have seen that a character array is preferred over a String object for storing highly sensitive information such as user passwords in Java. But if you must use a string to store a password, then you can use any of the following methods to validate it:
1. Using OWASP Validator
We can use OWASP Validation Regex, which is considered to be very safe. The regular expression requires the password to have 4 to 8 characters and should contain numbers, lowercase and uppercase letters.
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$
Following is the breakdown of each component:
(?=.*\d) # a digit must occur at least once
(?=.*[a-z]) # a lower case letter must occur at least once
(?=.*[A-Z]) # an upper case letter must occur at least once
.{4,8} # 4-8 character password, both inclusive
$ # end of the string
Here’s the complex version, which requires a password to have 4 to 32 characters. The password should satisfy at least 3 out of 4 conditions (uppercase and lowercase letters, numbers, and special characters) and should not have more than 2 equal characters in a row.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.regex.Pattern; // Java program to validate a password in Java class Main { // 4-8 character password requiring numbers and alphabets of both cases private static final String PASSWORD_REGEX = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$"; // 4-32 character password requiring at least 3 out of 4 (uppercase // and lowercase letters, numbers & special characters) and at-most // 2 equal consecutive chars. private static final String COMPLEX_PASSWORD_REGEX = "^(?:(?=.*\\d)(?=.*[A-Z])(?=.*[a-z])|" + "(?=.*\\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|" + "(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|" + "(?=.*\\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))(?!.*(.)\\1{2,})" + "[A-Za-z0-9!~<>,;:_=?*+#.\"&§%°()\\|\\[\\]\\-\\$\\^\\@\\/]" + "{8,32}$"; private static final Pattern PASSWORD_PATTERN = Pattern.compile(COMPLEX_PASSWORD_REGEX); public static void main(String[] args) { String password = "Stream@Java8"; // Validate a password if (PASSWORD_PATTERN.matcher(password).matches()) { System.out.print("The Password " + password + " is valid"); } else { System.out.print("The Password " + password + " isn't valid"); } } } |
Output:
The Password Stream@Java8 is valid
2. Using Another Regular Expression
Here’s another regular expression for validating a password, taken from the Stack Overflow thread. This is basically an extension of OWASP Regex seen before. Since every rule is an independent “module”, we can easily add, modify, or remove individual rules.
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,}$
Following is the detailed explanation:
(?=.*[0-9]) # a digit must occur at least once
(?=.*[a-z]) # a lower case letter must occur at least once
(?=.*[A-Z]) # an upper case letter must occur at least once
(?=.*[@#$%^&+=]) # a special character must occur at least once
(?=\\S+$) # no whitespace allowed in the entire string
.{8,16} # 8-16 character password, both inclusive
$ # end of the string
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.regex.Pattern; // Java program to validate a password in Java class Main { // 8-16 characters password with at least one digit, at least one // lowercase letter, at least one uppercase letter, at least one // special character with no white spaces private static final String PASSWORD_REGEX = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,16}$"; private static final Pattern PASSWORD_PATTERN = Pattern.compile(PASSWORD_REGEX); public static void main(String[] args) { String password = "Java#@#8"; // Validate a password if (PASSWORD_PATTERN.matcher(password).matches()) { System.out.print("The Password " + password + " is valid"); } else { System.out.print("The Password " + password + " isn't valid"); } } } |
Output:
The Password Java#@#8 is valid
That’s all about validating a password in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)