Java program to validate a password.

Java program to validate a password. Below is a simple Java program that validates a password based on certain criteria, along with a step-by-step explanation of how it works.

Java Program to Validate a Password

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

public class PasswordValidator {

    // Method to validate password
    public static boolean validatePassword(String password) {
        // Regular expression for password validation
        String passwordPattern = "^(?=.[0-9])(?=.[a-z])(?=.[A-Z])(?=.[@$%^&+=!])(?=\\S+$).{8,}$";
        
        Pattern pattern = Pattern.compile(passwordPattern);
        Matcher matcher = pattern.matcher(password);
        
        return matcher.matches();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

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

        // Validate the password
        if (validatePassword(password)) {
            System.out.println("Password is valid.");
        } else {
            System.out.println("Password is invalid. It must meet the following criteria:");
            System.out.println("1. At least 8 characters long");
            System.out.println("2. At least one digit");
            System.out.println("3. At least one lowercase letter");
            System.out.println("4. At least one uppercase letter");
            System.out.println("5. At least one special character (@$%^&+=!)");
            System.out.println("6. No whitespace characters");
        }

        scanner.close();
    }
}

Step-by-Step Explanation

1. Importing Required Classes:
We import “Scanner” for taking input from the user and “Pattern” and “Matcher” for regex matching.

2. Password Validator Class:
We create a class named “PasswordValidator”.

3. Password Validation Method:
We define a static method validatePassword(String password) that takes a password as input.
Inside this method, we define a regex pattern for password validation:
“^” asserts the start of the string.
“(?=.[0-9])” requires at least one digit.
“(?=.[a-z])” requires at least one lowercase letter.
“(?=.[A-Z])” requires at least one uppercase letter.
“(?=.[@$%^&+=!])” requires at least one special character.
“(?=\\S+$)” ensures there are no whitespace characters.
“.{8,}” checks that the total length is at least 8 characters.

– We compile this pattern and create a matcher for the provided password.
– The  matcher.matches() method returns true if the password meets all criteria, and false otherwise.

4. Main Method:
– We create a “main” method to execute the program.
– We instantiate a `Scanner` object to read user input.
– We call `validatePassword` with the input password and check the result.
– Based on the validation result, we print whether the password is valid or invalid and provide the criteria for a valid password.

5. Closing the Scanner:
– We close the scanner to free up resources.

Usage

  1. Upon executing the program, you will be asked to input a password.
  2.   It will check the password against the specified rules and inform you whether it is valid or not, along with the requirements if it is invalid.

Conclusion

This program demonstrates how to validate a password using regular expressions in Java. You can modify the regex pattern to fit different password policies as needed.

Leave a Reply

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