Skip to content
Advertisement

How to check password for number of lowercase letters entered?

I am using the below script to check my passwords for length, number of uppercase letters and number of digits.

Is there a way to make it also check for the number of lowercase letters? I tried to revamp my code a few times but each time I do it kicks out the other two checks. Thanks in advance!

import java.util.*;
public class ValidatePassword {
public static void main(String[] args) {
    String inputPassword;
    Scanner input = new Scanner(System.in);
    boolean success=false;
    while(!success){

    System.out.print("Password: ");
    inputPassword = input.next();
    System.out.println(PassCheck(inputPassword));
    if(PassCheck(inputPassword).equals("Valid Password")) success = true; 
    System.out.println("");

    } 
  }

  public static String PassCheck(String Password) {
    String result = "Valid Password";
    int length = 0;
    int numCount = 0;
    int capCount = 0;
    for (int x = 0; x < Password.length(); x++) {
      if ((Password.charAt(x) >= 47 && Password.charAt(x) <= 58) || (Password.charAt(x) >= 64 && Password.charAt(x) <= 91) ||
        (Password.charAt(x) >= 97 && Password.charAt(x) <= 122)) {
      } else {
        result = "Password Contains Invalid Character!";
      }
      if ((Password.charAt(x) > 47 && Password.charAt(x) < 58)) {
        numCount++;
      }
      if ((Password.charAt(x) > 64 && Password.charAt(x) < 91)) {
        capCount++;
      }
      
      length = (x + 1);
    }
    if (numCount < 2) {
      result = "digits";
    }
    if (capCount < 2) {
      result = "uppercase letters";
    }
    if (capCount < 2) {
      result = "uppercase letters";
    }
    if (numCount < 2 && capCount < 2) 
    {
      result = "uppercase letters digits";
    }

    if (length < 2) {
      result = "Password is Too Short!";
    }
    return (result);
  }
}

Advertisement

Answer

Checking each character one by one is a tedious task and will increase your logical error. You can use Regular Expressions for this. Your modified ‘PassCheck’ method:-

public static String PassCheck(String Password) {
    int length = Password.length();
    if(length<2)
        return "Password is Too Short!";
    String regex = "^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$";
    boolean d = Password.replaceAll("[^0-9]", "").length()<2;
    boolean u = Password.replaceAll("[^A-Z]", "").length()<2;
    boolean l = Password.replaceAll("[^a-z]", "").length()<2;
    if(d && u)
        return "digits uppercase";
    else if(l&&u)
        return "lowercase uppercase";
    else if(l&&d)
        return "lowercase digits";
    else if(d)
        return "digits";
    else if(u)
        return "uppercase";
    else if(l)
        return "lowercase";
    else if(!(Password.matches(regex)))
        return "Password contains Invalid Character!";
    return "Valid Password";
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement