Password Validation Program in Java

 

Introduction:

Password security is a critical aspect of protecting personal information online. One of the most important tasks is to ensure that passwords meet certain criteria to prevent easy guessing or brute-force attacks. In this program, we will validate a password to ensure it meets the following criteria:

  • At least 8 characters long.
  • Contains at least one uppercase letter.
  • Contains at least one numerical digit.
  • Contains at least one special character (e.g., @, #, $, %, etc.).

Objective:

The objective of this program is to take a user-input password and validate it against the above-mentioned criteria. The program will then return a message indicating whether the password is valid or not based on the conditions provided.

Java Code:

        import java.util.Scanner;
        import java.util.regex.Matcher;
        import java.util.regex.Pattern;

        public class PasswordValidator {

            public static void main(String[] args) {
                // Create scanner object to get user input
                Scanner scanner = new Scanner(System.in);

                // Ask the user to input a password
                System.out.print("Enter a password to validate: ");
                String password = scanner.nextLine();

                // Validate the password using the validatePassword method
                if (validatePassword(password)) {
                    System.out.println("Password is valid.");
                } else {
                    System.out.println("Password is invalid.");
                }

                // Close the scanner object
                scanner.close();
            }

            // Method to validate the password based on the given criteria
            public static boolean validatePassword(String password) {
                // Regular expression pattern for password validation
                String regex = "^(?=.*[0-9])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=.{8,})";
                
                // Compile the regular expression
                Pattern pattern = Pattern.compile(regex);
                
                // Create a matcher object to compare the password against the pattern
                Matcher matcher = pattern.matcher(password);
                
                // Return true if the password matches the pattern, otherwise false
                return matcher.matches();
            }
        }

Program Explanation:

Here is a step-by-step explanation of the Java program:

  1. Scanner Object: We use the Scanner class to read user input from the console.
  2. Input Password: The user is prompted to enter a password that needs to be validated.
  3. Validation Method: The method validatePassword uses a regular expression (regex) to check if the password meets the required criteria. The regex ensures:
    • The password contains at least one numeric digit (\\d).
    • The password contains at least one uppercase letter ([A-Z]).
    • The password contains at least one special character from the specified set ([@#$%^&+=]).
    • The password has a minimum length of 8 characters (.{8,}).
  4. Output: Based on whether the password matches the regex, the program outputs either “Password is valid.” or “Password is invalid.”
  5. Closing Resources: The scanner object is closed after it is no longer needed to avoid resource leaks.

How to Run the Program:

  1. Copy the provided Java code into a file named PasswordValidator.java.
  2. Open your terminal or command prompt.
  3. Navigate to the directory where the PasswordValidator.java file is saved.
  4. Compile the Java code using the following command:
    javac PasswordValidator.java
  5. Run the compiled code using the following command:
    java PasswordValidator
  6. Enter a password when prompted, and the program will tell you if it’s valid or not based on the specified criteria.

 

Leave a Reply

Your email address will not be published. Required fields are marked *