密码不检查Java中的长度条件

时间:2016-10-01 08:20:03

标签: java validation passwords runtime-error conditional-statements

我一直在苦苦挣扎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;
            }
    }
}

1 个答案:

答案 0 :(得分:0)

你需要或在你的第一个条件不是和

        if(password.length() < 4 || password.length() > 11){

不是

        if(password.length() < 4 && password.length() > 11){