1

I am in the process of creating a java Web Application, to do enable users to create fair strong credentials I would like to prevent them from entering weak passwords. I'd like to follow the basic rules of it must contain at least eight characters and at least one number and one symbol. I would appreciate a point in the right direction, many thanks!

Chance212
  • 29
  • 5

2 Answers2

0

Two solutions :

1.) You can use regular expression for the password requirements and compare the password entered by the user with that expression, if the pattern of password matches the regular expression then you can let the user register otherwise you can show an error.

2.) When user enters a password, you can call a function which would check whether: a. password contains at least one number. b. password contains at least one special character. c. is the length of password entered by user greater than the minimum length etc...

this function will return true or false depending on whether password is accepted or not.

Hope this helps you.

Alok Gupta
  • 1,245
  • 1
  • 9
  • 28
0

You can check user's entered password by this java-code,

public static boolean isValidPassword(String userEnteredPassword) {

    boolean atleastOneUpper = false;
    boolean atleastOneLower = false;
    boolean atleastOneDigit = false;

    if (userEnteredPassword.length() < 8) { // If its less then 8 characters, its automatically not valid
        return false;
    }

    for (int i = 0; i < userEnteredPassword.length(); i++) { // Lets iterate over only once. Saving time
        if (Character.isUpperCase(userEnteredPassword.charAt(i))) {
            atleastOneUpper = true;
        }
        else if (Character.isLowerCase(userEnteredPassword.charAt(i))) {
            atleastOneLower = true;
        }
        else if (Character.isDigit(userEnteredPassword.charAt(i))) {
            atleastOneDigit = true;
        }
    }

    return (atleastOneUpper && atleastOneLower && atleastOneDigit); // Return true IFF the userEnteredPassword is atleast eight characters long, has atleast one upper, lower and digit
}
Vishal Gajera
  • 3,798
  • 3
  • 23
  • 49