密码不检查Java中的长度条件
问题描述:
我一直在努力四个小时左右,但我正在接受我的密码的字符串没有检查长度条件。我知道我在做一些愚蠢的错误,但不幸的是我无法弄清楚。最后,我决定从这里请专家来帮助我。密码不检查Java中的长度条件
PS:我正在写一个验证密码长度为4到11个字符的函数,它包含一个大写字符,一个小写字符,一个数字和一个特殊字符。
public void validPassword(){
String password;
boolean con = true;
Pattern[] passRegex = new Pattern[4];
{
passRegex[0] = Pattern.compile(".*[A-Z].*");
passRegex[1] = Pattern.compile(".*[a-z].*");
passRegex[2] = Pattern.compile(".*\\d.*");
passRegex[3] = Pattern.compile("[A-Za-z0-9]*");
}
while(con){
System.out.println("Enter Your Password Using Correct Format:");
password = input.next();
if(password.length() < 4 && password.length() > 11){
System.out.println("Your Password Should Be 4 To 11 Characters Long");
}
if(!passRegex[0].matcher(password).matches()){
System.out.println("Your Password Must Contain Atleast One UpperCase Letter");
}
if(!passRegex[1].matcher(password).matches()){
System.out.println("Your Password Must Contain Atleast One LowerCase Letter");
}
if(!passRegex[2].matcher(password).matches()){
System.out.println("Your Password Must Contain Atleast One Digit");
}
if(passRegex[3].matcher(password).matches()){
System.out.println("Your Password Must Contain Atleast One Special Character");
}
else{
System.out.println("Your Password Is Correct");
con = false;
}
}
}
答
需要
或你的第一个条件不和
if(password.length() < 4 || password.length() > 11){
不
if(password.length() < 4 && password.length() > 11){
OMG!看到我告诉你我正在犯一个愚蠢的错误。事实上,在编码四个小时后我已经筋疲力尽了。 非常感谢帮助。 –