Create Password Validator Python and Java | Codeityweb

Password Validator Python and Java

We already have a Post on Generating Random Password which will generate strong random password for you.

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

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 in python and java.

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:



Password Validator Java:

package BlogSpot;

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:


Password Validator



Password Validator



Password Validator



Password Validator






Password Validator Code Python:


def isValid(password):
    digitcount = 0
    specialcount = 0
    uppercase = False
    lowercase = False
    whitespace = False

    if len(password) >= 8:
        for i in range(len(password)):
            if password[i].isupper():
                uppercase = True
            elif password[i].islower():
                lowercase = True
            elif password[i].isdigit():
                digitcount += 1
            elif password[i] == " ":    
                 whitespace = True
            else:
                specialcount += 1

        if uppercase and lowercase and not whitespace and digitcount >= 2     and specialcount >= 2:
        return "Valid Password"

        elif not uppercase:
            return "Upper case Letter is needed"

        elif not lowercase:
            return "Lower case Letter is needed"

        elif whitespace:
            return "White space is not allowed"

        elif digitcount < 2:
            return "Atleast 2 numbers are needed"
        else:
            return "Atleast 2 special characters are needed"
    

    else:
        return "Length should be atleast 8"
 

password = input("Enter password :\n")
print(isValid(password))



Related posts:





Conclusion:

Thus We have Created our own Password Validator in python and java.

Post a Comment

0 Comments