gpt4 book ai didi

android - 密码模式与匹配器类不匹配

转载 作者:太空狗 更新时间:2023-10-29 16:29:08 25 4
gpt4 key购买 nike

我的密码应该是这样的:

密码应至少包含一个大写字母、一个小写字母、一位数字和一个特殊字符,且长度至少为八位字符

我使用的模式是:^(?=.*[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,}$

此正则表达式将检查以下规则:

  1. 至少一个大写字母
  2. 至少一个小写字母
  3. 至少一位数
  4. 至少一个特殊字符
  5. 长度至少为 8

关于android - 密码模式与匹配器类不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43649987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com