简体   繁体   English

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

[英]Password not checking length condition in Java

I have been struggling around for 4 hours but the string in which I am taking my password is not checking the length condition. 我已经苦苦挣扎了4个小时,但是我输入密码的字符串没有检查长度条件。 I know i am doing some stupid mistake but unfortunately I can't figure it out. 我知道我在犯一些愚蠢的错误,但不幸的是我无法弄清楚。 Finally I decided to ask from Experts here kindly help me with this. 最后,我决定向这里的专家询问,请对此提供帮助。

PS: I am writing a function to validate the password to be 4 to 11 characters long,contain one uppercase character,one lowercase character,one digit and one special character. 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;
            }
    }
}

you need or in your first condition not and 您需要或您的第一个条件不是

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

not

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM