【发布时间】:2017-04-27 06:17:35
【问题描述】:
我的密码应该是这样的:
"密码应至少包含一个大写字母、一个小写字母、一个数字和一个特殊字符,最少八个字符长度"
我使用的模式是:^(?=.*[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 中设置错误。
可能是什么问题?
谢谢。
【问题讨论】:
-
你的正则表达式没问题,你需要把
\\\\d替换成\\d -
你是对的,谢谢。
标签: android passwords pattern-matching matcher