How to Create Password Validator

We also have a Post on Storing the Random Strong Password At Secure Password Manager.

But Still if you want to have your own Password and want to check if it is Strong or not.For that we are going to create our Password Validator using Java and Python.

Conditions for A strong Password:

  1. Their should be atleast one Upper Case Letter.
  2. Atleast 2 numbers and 2 Special Characters.
  3. Their should be atleast one Lower Case Letter.
  4. Length of the Password Should be greater than 8 or atleast 8.
  5. Whitespace should not be their.

So without further discussion lets dive into the coding part:

Code Java:

import java.util.Scanner;

public class PasswordValidator {

public String isValid(String password) {

int specialcount = 0;

int digitcount = 0;

boolean uppercase = false;

boolean lowercase = false;

boolean whitespace = false;

if (password.length() >= 8) {

for (char i : password.toCharArray()) {

if (Character.isUpperCase(i)) {

uppercase = true;

} else if (Character.isLowerCase(i)) {

lowercase = true;

} else if (Character.isDigit(i)) {


} else if (Character.isWhitespace(i)) {

whitespace = true;

} else {




if (uppercase && lowercase && digitcount >= 2 && !whitespace && specialcount >= 2) {

return “Valid Password”;

} else if (!uppercase)

return “Atleast One Upper Case Character is Needed”;

else if (!lowercase)

return “Atleast One Lower Case Character is Needed”;

else if (digitcount < 2)

return “Atleast 2 numbers are needed”;

else if (specialcount < 2)

return “Atleast 2 special characters are needed”;


return “White space is not allowed”;

} else {

return “Length should be Atleast 8”;



public static void main(String args[]) {

Scanner sc = new Scanner(;

System.out.println(“Enter the Password”);

String password = sc.nextLine();

String response = new PasswordValidator().isValid(password);




Upper Case Letter Needed
Special Character is needed
Valid Password

A Website To Learn Coding,Get Mini Projects With Implementations ,Competitive Programming ,HackerRank Solutions.@