Password validate 8 digits, contains upper, lowercase, and a special character

Viewed 54563

So I wrote a method that makes the user enter a password and this password must pass the following specs:

1. Be at least 8 digits long

2. Have an uppercase

3. Have a lowercase

4. Have special digit

I'm not sure as to why when I input it, the output doesn't account for the special Character and throws an error.

Here is my code so far:

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.print("Please enter a given  password : ");
    String passwordhere = in.nextLine();
    System.out.print("Please re-enter the password to confirm : ");
    String confirmhere = in.nextLine();
    System.out.println("your password is: " + passwordhere);

    while (!passwordhere.equals(confirmhere) || !isValid(passwordhere)) {
        System.out.println("The password entered here  is invalid");
        System.out.print("Please enter the password again.it must be valid : ");
        String Passwordhere = in.nextLine();
        System.out.print("Please re-enter the password to confirm : ");

    }
}

public static boolean isValid(String passwordhere) {

    if (passwordhere.length() < 8) {
        return false;
    } else {

        for (int p = 0; p < passwordhere.length(); p++) {
            if (Character.isUpperCase(passwordhere.charAt(p))) {
            }
        }
        for (int q = 0; q < passwordhere.length(); q++) {
            if (Character.isLowerCase(passwordhere.charAt(q))) {
            }
        }
        for (int r = 0; r < passwordhere.length(); r++) {
            if (Character.isDigit(passwordhere.charAt(r))) {
            }
        }
        for (int s = 0; s < passwordhere.length(); s++) {
            if (Character.isSpecialCharacter(passwordhere.charAt(s))) {
            } 
            }
            return true;
        }
}

Also, another problem is for example, lets say the user enter bob123 as their password.

How can I get the loop to tell the user what It needs to be a correct password?

On the example above it is missing a Capital Letter and a symbol(*&^..etc).

How can I add this to print out each time the user makes a password and until they get the right password to pass all specs of the code?

5 Answers

Use this bean validation library for password validation:

https://github.com/ankurpathak/password-validation https://mvnrepository.com/artifact/com.github.ankurpathak.password/password-validation

It provides many constraint to deal with password validation and many more will be added in near future:

  1. ContainDigit: To validate if password contain specified number of digits.
  2. ContainLowercase: To validate if password contain specified number of lowercase.
  3. ContainSpecial: To validate if password contain specified number of special symbols.
  4. ContainUppercase: To validate if password contain specified number of uppercase.
  5. NotContainWhitespace: To validate should not have any whitespace.
  6. PasswordMatches: To validate if password and confirm password are equal. You can move also move constraint to confirm password field by using flag showErrorOnConfirmPassword (default is true).

All the constraints by default ignore blank so that it will be reported separately by NotBlank standard bean validation constraint and same can we turned of using ignoreBlank(true by default) flag of each constraint.

Small example to use the library is:


    @PasswordMatches
    public class PasswordDto {
     @Size(min = 8, max = 30)
     @NotContainWhitespace
     @ContainSpecial
     @ContainDigit
     private String password;
     @NotBlank
     private String confirmPassword;
    }

import javax.swing.JOptionPane;

    public class Validation {   

    static String password;

    public static boolean IsValidInput(String s) {

     boolean status = false;    
     char [] array = s.toCharArray();
     int lower=0, upper=0, digits=0;

     if (s.length() > 8) 
     status = true;

      for ( int i = 0;  i < array.length; i++) {
       if(Character.isDigit(array[i]))
          digits++;
       if(Character.isLowerCase(array[i]))
          lower++;
       if(Character.isUpperCase(array[i]))
          upper++;
     }

       if ( !(lower  > 0 ))
       status = false;

       if ( !(upper  > 0 ))
       status = false;

       if ( !(digits > 0 ))
       status = false;

       return status;
     }     

     public static void  setPassword(String p) {
     if (IsValidInput(p)) {
      password = p;
     JOptionPane.showMessageDialog( null, " Your Password is accepted -" + p);
     }

     else {
     password ="";
     JOptionPane.showMessageDialog( null, " Your  Password is  not accepted -" + p);
     }
     }

    }
Related