Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters

前端 未结 30 3728
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 04:28

I want a regular expression to check that:

A password contains at least eight characters, including at least one number and includes both lower and uppercase letter

30条回答
  •  爱一瞬间的悲伤
    2020-11-21 05:05

    According to your need this pattern should work just fine. Try this,

    ^(?=(.*\d){1})(.*\S)(?=.*[a-zA-Z\S])[0-9a-zA-Z\S]{8,}
    

    Just create a string variable, assign the pattern, and create a boolean method which returns true if the pattern is correct, else false.

    Sample:

    String pattern = "^(?=(.*\d){1})(.*\S)(?=.*[a-zA-Z\S])[0-9a-zA-Z\S]{8,}";
    String password_string = "Type the password here"
    
    private boolean isValidPassword(String password_string) {
        return password_string.matches(Constants.passwordPattern);
    }
    

提交回复
热议问题