在 android 中设置用于验证密码的密码模式

setting Password pattern for validation of Password in android

我想在 android 中创建包含以下内容的密码模式

我用过html5密码模式

String password_pattern = "(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$";

但它不起作用。

试试下面的正则表达式,它可以满足你的需要,

1.大写字母

2.小写字母

3. 特殊字符

4.个数

5.最少8位

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$

检查这个 link :

https://regex101.com/r/S7cd5A/1

你可以用这个。

private static final String PASSWORD_PATTERN = "((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";

模式描述

  (?=.*\d)      #   must contains one digit from 0-9
  (?=.*[a-z])   #   must contains one lowercase characters
  (?=.*[A-Z])   #   must contains one uppercase characters
  (?=.*[@#$%])   #   must contains one special symbols in the list "@#$%"
              . #   match anything with previous condition checking
  {6,20}        #   length at least 6 characters and maximum of 20

你可以这样使用

public boolean validate(final String password){

        return PASSWORD_PATTERN.matches(password);
}

详情请Check this