0

The issue I am having is when someone enters a password that includes digits, uppercase, and lowercase the program still says they need all them.

I need the program to list all the requirements for a password if they are not met.

For example:

A123456789aa returns: "You need at least one lowercase. Please Enter Your Password:"

It should return: "Enter termination key"

public static void main(String[] args) {

    Scanner input = new Scanner (System.in);

    boolean condition = true;
    while (condition) {
        System.out.print("Please Enter Your Password: ");
        String password = input.nextLine();

        if (password.equals("endofinput")) {
            System.out.print("Your password is valid"); condition = false;

        } else {
            System.out.print(passCheck(password));
            condition = true;
        }
    }
    input.close();
}


public static String passCheck(String password) {
    String specialChars = "/*!@#$%^&*()\"{}_[]|\\?/<>,." + " ";

      if (password.length() < 8) {
        return ("You need at least 8 characters. ");

    } for (int i = 0; i < password.length(); i++) {
        if (specialChars.contains(password.substring(i)))
            return ("Your password cannot contain special characters. ");
        else if (password.equals("password"))
            return ("Your password cannot be password. ");
        else if(!Character.isUpperCase(password.charAt(i)))
            return ("You need at least one uppercase. ");
        else if (!Character.isLowerCase(password.charAt(i)))
            return ("You need at least one lowercase. ");
        else if (!Character.isDigit(password.charAt(i)))
            return ("You need at least one digit. ");   
    }
return "Enter termination key";
}
  • 1
    This link https://stackoverflow.com/questions/3802192/regexp-java-for-password-validation explains how you can use regex to validate password inputs with many restrictions – Diego Victor de Jesus Jun 07 '18 at 21:40

2 Answers2

3

your passCheck method iterates over all chars, and returns a result if one char does not fullfill one of your requirements. An alternative would be to assume that the input is not valid, until all requirements are fullfilled:

boolean foundUppercase = false;
    boolean foundLowercase = false;
    boolean foundDigits = false;
    //Dont have to do the check for password as input for every letter in the word.
    if (password.equals("password"))
        return ("Your password cannot be password. ");

    for (int i = 0; i < password.length(); i++) {
        if (specialChars.contains(password.substring(i)))
            //only in this case we can for sure say that the input is not valid
            return ("Your password cannot contain special characters. ");
        else if (Character.isUpperCase(password.charAt(i)))
            foundUppercase=true;
        else if (Character.isLowerCase(password.charAt(i)))
            foundLowercase= true;
        else if (Character.isDigit(password.charAt(i)))
            foundDigits = true;
    }
    if (!foundUppercase) {
        // uppercase letter missing
        return ("You need at least one uppercase. ");
    } else if (!foundLowercase) {
        // lower case letter missing
        return ("You need at least one lowercase. ");
    } else if (!foundDigits) {
        // missing digits
        return ("You need at least one digit. ");
    }

    return "Enter termination key";
Barry
  • 337
  • 1
  • 2
  • 15
  • Thanks to a fellow user, whose comment i cannot see again I edited my post and removed missplaced '!'- Symbols – Barry Jun 07 '18 at 21:53
  • I removed it as you fixed it and I'm half asleep – dave Jun 07 '18 at 21:58
  • This solution still causes the problem to persist. enter password: a123456789 returns: "You need at least one lowercase. " – Jonathan Baloney Jun 07 '18 at 22:06
  • Have seen my edit? where I have removed the '!' symbol at the line if (Character.isUpperCase(password.charAt(i))) ? In my test your provided password returns: "You need at least one uppercase. " – Barry Jun 07 '18 at 22:07
  • I have made the edit already, but the error still occurs. password entered: aAbaAvbede1254 returns: "You need at least one lowercase. " – Jonathan Baloney Jun 07 '18 at 22:12
  • Oh I made an other mistake during copy paste. Sry. I edited my answer, and changed if (Character.isLowerCase(password.charAt(i))) foundDigits = true; to if (Character.isLowerCase(password.charAt(i))) foundLowercase= true; – Barry Jun 07 '18 at 22:13
  • What is the process for making password.equals "password" but instead of password it covers, PassworD or passWORd etc.. – Jonathan Baloney Jun 07 '18 at 22:59
  • password.toLowerCase().equals("password"); This would normalize the provided input to lower case letters, which makes it easier to check for equal to "password". But make sure to not overwrite the provided password variable, since that would cause " you need at least one uppercase." message – Barry Jun 08 '18 at 09:22
1

You can use this validation function

public static String passCheck(String password) {
    //String specialChars = "/*!@#$%^&*()\"{}_[]|\\?/<>,." + " ";
    String expected_pattern = "^[a-zA-Z0-9]{8,}$";
    String lowercase_pattern = "(.*)[a-z]+(.*)";
    String uppercase_pattern = "(.*)[A-Z]+(.*)";
    String digit_pattern = "(.*)[0-9]+(.*)";

    if (password == null || password.length() < 8) return ("You need at least 8 characters. ");     
    if (password.toLowerCase().equals("password")) return ("Your password cannot be password. ");
    if (!password.matches(lowercase_pattern)) return ("You need at least one lowercase. ");
    if (!password.matches(uppercase_pattern)) return ("You need at least one uppercase. ");
    if (!password.matches(digit_pattern)) return ("You need at least one digit. ");
    if (!password.matches(expected_pattern)) return ("Your password cannot contain special characters. ");

    return "Enter termination key";
}