如何通过允许特定字符验证EditText
Regex
。
我的条件是:
密码规则:
一个符号 (@,$,%,&,#,)
任何可接受的正常符号。
我可以知道实现目标的正确方法是什么吗?
答案 0 :(得分:76)
尝试这可能有帮助
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{4,}$
如何运作?
^ # start-of-string
(?=.*[0-9]) # a digit must occur at least once
(?=.*[a-z]) # a lower case letter must occur at least once
(?=.*[A-Z]) # an upper case letter must occur at least once
(?=.*[@#$%^&+=]) # a special character must occur at least once you can replace with your special characters
(?=\\S+$) # no whitespace allowed in the entire string
.{4,} # anything, at least six places though
$ # end-of-string
如何实施?
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = (EditText) findViewById(R.id.edtText);
Button btnCheck = (Button) findViewById(R.id.btnCheck);
btnCheck.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (isValidPassword(editText.getText().toString().trim())) {
Toast.makeText(MainActivity.this, "Valid", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "InValid", Toast.LENGTH_SHORT).show();
}
}
});
}
public boolean isValidPassword(final String password) {
Pattern pattern;
Matcher matcher;
final String PASSWORD_PATTERN = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{4,}$";
pattern = Pattern.compile(PASSWORD_PATTERN);
matcher = pattern.matcher(password);
return matcher.matches();
}
}
答案 1 :(得分:8)
试试这个。
(/^(?=.*\d)(?=.*[A-Z])([@$%&#])[0-9a-zA-Z]{4,}$/)
(/^
(?=.*\d) //should contain at least one digit
(?=.*[@$%&#]) //should contain at least one special char
(?=.*[A-Z]) //should contain at least one upper case
[a-zA-Z0-9]{4,} //should contain at least 8 from the mentioned characters
$/)
答案 2 :(得分:4)
对于 Kotlin 爱好者:
$bar.css({ width: percent + "%" });
答案 3 :(得分:4)
以上都不适合我。
对我有用的东西
fun isValidPasswordFormat(password: String): Boolean {
val passwordREGEX = Pattern.compile("^" +
"(?=.*[0-9])" + //at least 1 digit
"(?=.*[a-z])" + //at least 1 lower case letter
"(?=.*[A-Z])" + //at least 1 upper case letter
"(?=.*[a-zA-Z])" + //any letter
"(?=.*[@#$%^&+=])" + //at least 1 special character
"(?=\\S+$)" + //no white spaces
".{8,}" + //at least 8 characters
"$");
return passwordREGEX.matcher(password).matches()
}
希望它对某人有帮助。
答案 4 :(得分:3)
try {
if (subjectString.matches("^(?=.*[@$%&#_()=+?»«<>£§€{}\\[\\]-])(?=.*[A-Z])(?=.*[a-z])(?=.*\\d).*(?<=.{4,})$")) {
// String matched entirely
} else {
// Match attempt failed
}
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
}
(?=.*[@\$%&#_()=+?»«<>£§€{}.[\]-]) -> must have at least 1 special character
(?=.*[A-Z]) -> Must have at least 1 upper case letter
(?=.*[a-z]) -> Must have at least 1 lower case letter
(?=.*\\d) -> Must have at least 1 digit
(?<=.{4,})$") -> Must be equal or superior to 4 chars.
答案 5 :(得分:2)
对于每种检查格式,您都可以使用Patern
课程而不是Matcher
。
我给你一个例子:
Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+");
Matcher matcher = pattern.matcher(myEmailString);
if (!myEmailString.contains("@") || !matcher.matches()) {
// error in the email : do staff
myEmailView.setError("invalid email !");
}
答案 6 :(得分:1)
除了已经给出的答案之外,我还建议使用其他方法来识别特殊字符,并且还会对不同规则进行检查。
首先将其拆分:无需制定一个大规则,而是将其拆分并分别检查每个规则,以便您可以向用户提供有关其密码到底有什么问题的反馈。这可能需要更长的时间,但是在密码检查之类的过程中,这将不会引起注意。而且,这种方式更易于阅读。
第二,您可以将其翻转并检查密码是否包含既不是拉丁字母(a-zA-Z)也不是数字(0-9)的字符,而不是检查特殊字符列表。这样,您就不会“忘记”特殊字符。例如,假设您专门进行检查,但您却忘记了诸如“ {”之类的字符。使用这种方法,就不会发生这种情况。您可以通过将您认为不是特殊字符的内容扩展到该列表,例如例如一个空格。在kotlin
中,它看起来像这样:
val errorText = when {
/* Rule 1 */
!password.contains(Regex("[A-Z]")) -> "Password must contain one capital letter"
/* Rule 2 */
!password.contains(Regex("[0-9]")) -> "Password must contain one digit"
/* Rule 3, not counting space as special character */
!password.contains(Regex("[^a-zA-Z0-9 ]")) -> "Password must contain one special character"
else -> null
}
根据您的编码,您还可以使用正则表达式并使用
之类的十六进制代码范围来定义特殊字符。Reges("[\x00-\x7F]")
答案 7 :(得分:1)
所有其他答案都很好,但是特殊字符的实现对于我的口味来说太混乱了。我改用Unicode代替特殊字符。
我使用了OWASP website中指定的特殊字符。
科特琳:
val SPECIAL_CHARACTERS_REGEX =
"?=.*[\\u0020-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E]"
val PASSWORD_REGEX =
"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])($SPECIAL_CHARACTERS_REGEX).{8,}\$"
fun isValidPassword(password: String) = Pattern.matches(PASSWORD_REGEX, password)
答案 8 :(得分:0)
我来不及回答,但仍然可以为您提供帮助。
我曾经与Kotlin
合作。
添加以下功能。
private fun isValidPassword(password: String): Boolean {
val pattern: Pattern
val matcher: Matcher
val specialCharacters = "-@%\\[\\}+'!/#$^?:;,\\(\"\\)~`.*=&\\{>\\]<_"
val PASSWORD_REGEX = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[$specialCharacters])(?=\\S+$).{8,20}$"
pattern = Pattern.compile(PASSWORD_REGEX)
matcher = pattern.matcher(password)
return matcher.matches()
}
功能描述:
您可以根据需要对其进行修改。
希望有帮助。
答案 9 :(得分:0)
最常用的密码验证是
正则表达式:
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[\\\/%§"&“|`´}{°><:.;#')(@_$"!?*=^-]).{8,}$
科特琳代码:
val PASSWORD_REGEX_PATTERN = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[\\\/%§"&“|`´}{°><:.;#')(@_$"!?*=^-]).{8,}$"
fun isValidPassword(password: String?): Boolean {
val pattern: Pattern =
Pattern.compile(PASSWORD_REGEX_PATTERN)
val matcher: Matcher = pattern.matcher(password)
return matcher.matches()
}
在线正则表达式验证器进行检查: