Write a Java Program to validate passwords in Java. Password should have some format like “Hello#123“. Let us write a java program to validate passwords using regex and without regex.
Rules to Validate Password
- Must contain at least one digit
- Must contain at least one of the following special characters @, #, $
- The length should be between 6 to 20 characters.
If the password is as per the given rules return 1 else return -1. If the return value is 1 then print the valid password else print it as the invalid password.
Sample Input and Output Format to validate password
- Input is a string.
- The output is a string.
Sample Input 1:
%Dhoom%
Sample Output 1:
Invalid password
Sample Input 2:
#@6Don
Sample Output 2:
Valid password
Program to validate password in Java with regex
Following are the steps for password validations in Java:
- Input password as a string.
- Call method Validation with the input string.
- Here, in this Validation method, use the regex pattern to check if the password matches the said rule above.
- If it matches, return integer value 1 else return -1.
package com.company; import java.util.Scanner; public class PasswordValidation { public static void main(String[] args) { System.out.println("Enter password :"); Scanner scanner = new Scanner(System.in); String s1 = scanner.nextLine(); int n = Validation(s1); if(n == 1){ System.out.println("It is a valid password"); } else { System.out.println("It is not a valid password"); } } private static int Validation(String s1) { if(s1.matches(".*[0-9]{1,}.*") && s1.matches(".*[@#$]{1,}.*") && s1.length() >=6 && s1.length()<=20){ return 1; } else { return -1; } } }
Output
Validation of Password without using regex
Following are the steps for password validations in Java without regex:
- Input a string.
- Convert string to character array using toCharArray() method.
- Pass the character array to the validatePassword() method.
- Inside the method, first, declare a variable flag with the value 0.
- Now, check for the following conditions
- First, check if the length is between 6 and 20.
- Then, check if it contains at least one digit along with a special character.
- If, not found then, break from the loop and print “Invalid” else print “Valid”.
package com.date; import java.util.Scanner; public class PasswordValidationWithoutRegex { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); char c[] = str.toCharArray(); System.out.println(validatePassword(c)); } private static String validatePassword(char c[]) { int flag = 0; if(c.length < 6 || c.length > 20){ return "Invalid"; } for(int i = 0 ; i < c.length ; i++){ if(c[i] == '@' || c[i] == '#' || c[i] == '$'|| c[i] == '0' || c[i] == '1' || c[i] == '2' || c[i] == '3' || c[i] == '4' || c[i] == '5' || c[i] == '6'|| c[i] == '7'|| c[i] == '8'|| c[i] == '9'){ flag = 0; break; } flag = 1; if(flag == 1){ return "Invalid"; } } return "Valid"; } }
Output
Strong password Suggestion in Java
Obtain a length and suggest strong passwords to the user. To suggest a password, we will use the following rules:
- The password should be generated of the length specified.
- The first character should be upper case.
- The second character should be lower case.
- The third character should be a special case.
- The fourth character should be a number. And, the remaining length should be a combination of all.
Java Program to generate Strong Password
Following are the steps to generate a strong password:
- Input the length from the user.
- Pass the length to generateStrongPassword() method.
- Inside the method, initialize lower case, upper case, special characters, and number.
- Use the Random class to generate random numbers.
- Create an array of characters of a specified length.
- Initialize the 0th, 1st, 2nd, 3rd position with random upper case, lower case, special characters, and number.
- Now, iterate over the remaining length of the character array and generate random strings, and at last combine all of them together.
package com.date; import java.util.Random; import java.util.Scanner; public class StrongPassword { public static void main(String[] args) { System.out.println("Enter Password "); Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); System.out.println(generateStrongPassword(length)); } private static char[] generateStrongPassword(int l) { String u_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String l_case = "abcdefghijklmnopqrstuvwxyz"; String s_case = "#@$"; String num = "0123456789"; // generate number Random random = new Random(); char[] password = new char[l]; password[0] = u_case.charAt(random.nextInt(u_case.length())); password[1] = l_case.charAt(random.nextInt(l_case.length())); password[2] = s_case.charAt(random.nextInt(s_case.length())); password[3] = num.charAt(random.nextInt(num.length())); for(int i =4 ;i < l; i++){ password[i] = (u_case+l_case+s_case+num).charAt(random.nextInt((u_case+l_case+s_case+num).length())); } return password; } }
Output
In this way, we learned how to validate passwords in Java using regular expressions and without regular expressions along with strong password generation.