How to Create Password Validator

Codeityweb
2 min readJan 5, 2021

--

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)) {

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:

Upper Case Letter Needed
Special Character is needed
Valid Password

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Codeityweb
Codeityweb

Written by Codeityweb

A Website To Learn Coding,Get Mini Projects With Implementations ,Competitive Programming ,HackerRank Solutions.@ https://codeityweb.blogspot.com

No responses yet

Write a response