Java Regex Password Validate passwordIsValid(String password)

Here you can find the source of passwordIsValid(String password)

Description

Validates whether a password is valid.

License

Open Source License

Parameter

Parameter Description
password the password.

Return

true if the password is valid, false otherwise.

Declaration

public static boolean passwordIsValid(String password) 

Method Source Code

//package com.java2s;

import java.util.regex.Pattern;

public class Main {
    private static final Pattern DIGIT_PATTERN = Pattern.compile(".*\\d.*");
    private static final Pattern UPPERCASE_PATTERN = Pattern
            .compile(".*[A-Z].*");

    /**/*from  w  w  w. ja v a 2 s.  co  m*/
     * Validates whether a password is valid. A password must:
     * <p>
     * <ul>
     * <li>Be between 8 and 80 characters long</li>
     * <li>Include at least one digit</li>
     * <li>Include at least one uppercase letter</li>
     * </ul>
     *
     * @param password the password.
     * @return true if the password is valid, false otherwise.
     */
    public static boolean passwordIsValid(String password) {
        if (password == null || password.trim().length() < 8
                || password.trim().length() > 35) {
            return false;
        }

        return DIGIT_PATTERN.matcher(password).matches()
                && UPPERCASE_PATTERN.matcher(password).matches();
    }
}

Related

  1. passwordContainInvalidCharacter(String password)
  2. passwordValidation()
  3. passwordValidation(String password)
  4. passwordValidator(String password)