How to validate a Password using Regular Expressions in Java - GeeksforGeeks

16 Apr, 2020
Given a password, the task is to validate the password with the help of Regular Expression.A password is considered valid if all the following constraints are satisfied:
It contains at least 8 characters and at most 20 characters.
It contains at least one digit.
It contains at least one upper case alphabet.
It contains at least one lower case alphabet.
It contains at least one special character which includes !@#$%&*()-+=^.
It doesn’t contain any white space.
Examples:
Input: Str = “Geeks@portal20”Output: True.Explanation: This password satisfies all constraints mentioned above.
Input: Str = “Geeksforgeeks”Output: False.Explanation: It contains upper case and lower case alphabet but doesn’t contains any digits, and special characters.
Input: Str = “Geeks@ portal9”Output: False.Explanation: It contains upper case alphabet, lower case alphabet, special characters, digits along with white space which is not valid.
Input: Str = “12345”Output: False.Explanation: It contains only digits but doesn’t contains upper case alphabet, lower case alphabet, special characters, and 8 characters.
Approach: This problem can be solved by using Regular Expression.
Get the password.Create a regular expression to check the password is valid or not as mentioned below:regex = “^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&-+=()])(?=\\S+$).{8, 20}$”where:^ represents starting character of the string.(?=.*[0-9]) represents a digit must occur at least once.(?=.*[a-z]) represents a lower case alphabet must occur at least once.(?=.*[A-Z]) represents an upper case alphabet that must occur at least once.(?=.*[@#$%^&-+=()] represents a special character that must occur at least once.(?=\\S+$) white spaces don’t allowed in the entire string..{8, 20} represents at least 8 characters and at most 20 characters.$ represents the end of the string.Match the given string with the Regex. In java, this can be done using Pattern.matcher().Return true if the string matches with the given regex, else return false.
Get the password.
Create a regular expression to check the password is valid or not as mentioned below:regex = “^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&-+=()])(?=\\S+$).{8, 20}$”where:^ represents starting character of the string.(?=.*[0-9]) represents a digit must occur at least once.(?=.*[a-z]) represents a lower case alphabet must occur at least once.(?=.*[A-Z]) represents an upper case alphabet that must occur at least once.(?=.*[@#$%^&-+=()] represents a special character that must occur at least once.(?=\\S+$) white spaces don’t allowed in the entire string..{8, 20} represents at least 8 characters and at most 20 characters.$ represents the end of the string.
regex = “^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&-+=()])(?=\\S+$).{8, 20}$”
where:
^ represents starting character of the string.
(?=.*[0-9]) represents a digit must occur at least once.
(?=.*[a-z]) represents a lower case alphabet must occur at least once.
(?=.*[A-Z]) represents an upper case alphabet that must occur at least once.
(?=.*[@#$%^&-+=()] represents a special character that must occur at least once.
(?=\\S+$) white spaces don’t allowed in the entire string.
.{8, 20} represents at least 8 characters and at most 20 characters.
$ represents the end of the string.
Match the given string with the Regex. In java, this can be done using Pattern.matcher().
Return true if the string matches with the given regex, else return false.
Below is the implementation of the above approach:
// Java program to validate// the password using ReGex  import java.util.regex.*;class GFG {      // Function to validate the password.    public static boolean    isValidPassword(String password)    {          // Regex to check valid password.        String regex = "^(?=.*[0-9])"                       + "(?=.*[a-z])(?=.*[A-Z])"                       + "(?=.*[@#$%^&+=])"                       + "(?=\\S+$).{8,20}$";          // Compile the ReGex        Pattern p = Pattern.compile(regex);          // If the password is empty        // return false        if (password == null) {            return false;        }          // Pattern class contains matcher() method        // to find matching between given password        // and regular expression.        Matcher m = p.matcher(password);          // Return if the password        // matched the ReGex        return m.matches();    }      // Driver Code.    public static void main(String args[])    {          // Test Case 1:        String str1 = "Geeks@portal20";        System.out.println(isValidPassword(str1));          // Test Case 2:        String str2 = "Geeksforgeeks";        System.out.println(isValidPassword(str2));          // Test Case 3:        String str3 = "Geeks@ portal9";        System.out.println(isValidPassword(str3));          // Test Case 4:        String str4 = "1234";        System.out.println(isValidPassword(str4));          // Test Case 5:        String str5 = "Gfg@20";        System.out.println(isValidPassword(str5));          // Test Case 6:        String str6 = "geeks@portal20";        System.out.println(isValidPassword(str6));    }}
true
false
false
false
false
false

java-regular-expression
Java
Java Programs
Strings
Strings
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Stream In Java
Interfaces in Java
How to iterate any Map in Java
Initializing a List in Java
Convert a String to Character Array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class