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:
- Scanner Object: We use the
Scannerclass to read user input from the console. - Input Password: The user is prompted to enter a password that needs to be validated.
- Validation Method: The method
validatePassworduses 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,}).
- The password contains at least one numeric digit (
- Output: Based on whether the password matches the regex, the program outputs either “Password is valid.” or “Password is invalid.”
- Closing Resources: The
scannerobject is closed after it is no longer needed to avoid resource leaks.
How to Run the Program:
- Copy the provided Java code into a file named
PasswordValidator.java. - Open your terminal or command prompt.
- Navigate to the directory where the
PasswordValidator.javafile is saved. - Compile the Java code using the following command:
javac PasswordValidator.java
- Run the compiled code using the following command:
java PasswordValidator
- Enter a password when prompted, and the program will tell you if it’s valid or not based on the specified criteria.

