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:
- Their should be atleast one Upper Case Letter.
- Atleast 2 numbers and 2 Special Characters.
- Their should be atleast one Lower Case Letter.
- Length of the Password Should be greater than 8 or atleast 8.
- 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)) {
digitcount++;
} else if (Character.isWhitespace(i)) {
whitespace = true;
} else {
specialcount++;
}
}
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”;
else
return “White space is not allowed”;
} else {
return “Length should be Atleast 8”;
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the Password”);
String password = sc.nextLine();
String response = new PasswordValidator().isValid(password);
System.out.println(response);
sc.close();
}
}
Output:



Visit Below Post to Get code in Python:
Thank you for reading!