作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的密码应该是这样的:
“密码应至少包含一个大写字母、一个小写字母、一位数字和一个特殊字符,且长度至少为八位字符”
我使用的模式是:^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$ #!%*?&])[A-Za-z\\d$@$#!%*?&]{8,}
因此,我在我的 Constant.java 文件中创建了如下函数:
public static Boolean passwordMatcher(TextInputLayout edtText,String string) {
Pattern pattern = Pattern.compile("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\\\d)(?=.*[$@$#!%*?&])[A-Za-z\\\\d$@$#!%*?&]{8,}");
Matcher matcher = pattern.matcher(edtText.getEditText().getText().toString());
boolean isMatched = matcher.matches();
if (isMatched) {
return true;
}
if (!isMatched) {
edtText.setErrorEnabled(true);
edtText.setError("" + string);
edtText.setFocusable(true);
return false;
}
return true;
}
在我的 MainActivity.java 文件中,我检查了如下验证:
if (!Constant.passwordMatcher(edtPassword, mContext.getResources().getString(R.string.error_activity_signup_password_invalid))) {
return;
}
但是,即使我尝试过,我也没有成功:'Jaimin123#' 作为我的密码。总是在我的 TextInputLayout 中设置错误。
可能是什么问题?
谢谢。
最佳答案
尝试使用以下正则表达式进行密码匹配。
^(?=.*?[A-Z])(?=(.*[a-z]){1,})(?=(.*[\d]){1,})(?=(.*[\W]){1,})(?!.*\s).{8,}$
此正则表达式将检查以下规则:
关于android - 密码模式与匹配器类不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43649987/
我是一名优秀的程序员,十分优秀!