[英]Password not checking length condition in Java
我已經苦苦掙扎了4個小時,但是我輸入密碼的字符串沒有檢查長度條件。 我知道我在犯一些愚蠢的錯誤,但不幸的是我無法弄清楚。 最后,我決定向這里的專家詢問,請對此提供幫助。
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){
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.