密码验证8位数,包含upper,lowerCase和特殊字符

时间:2016-03-19 02:20:07

标签: java validation passwords

所以我写了一个让用户输入密码的方法,这个密码必须通过以下规范:

1。长度至少为8位

2. 大写

3. 小写

4. 有特殊数字

我不确定为什么当我输入它时,输出不考虑特殊字符并抛出错误。

到目前为止,这是我的代码:

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.print("Please enter a given  password : ");
    String passwordhere = in.nextLine();
    System.out.print("Please re-enter the password to confirm : ");
    String confirmhere = in.nextLine();
    System.out.println("your password is: " + passwordhere);

    while (!passwordhere.equals(confirmhere) || !isValid(passwordhere)) {
        System.out.println("The password entered here  is invalid");
        System.out.print("Please enter the password again.it must be valid : ");
        String Passwordhere = in.nextLine();
        System.out.print("Please re-enter the password to confirm : ");

    }
}

public static boolean isValid(String passwordhere) {

    if (passwordhere.length() < 8) {
        return false;
    } else {

        for (int p = 0; p < passwordhere.length(); p++) {
            if (Character.isUpperCase(passwordhere.charAt(p))) {
            }
        }
        for (int q = 0; q < passwordhere.length(); q++) {
            if (Character.isLowerCase(passwordhere.charAt(q))) {
            }
        }
        for (int r = 0; r < passwordhere.length(); r++) {
            if (Character.isDigit(passwordhere.charAt(r))) {
            }
        }
        for (int s = 0; s < passwordhere.length(); s++) {
            if (Character.isSpecialCharacter(passwordhere.charAt(s))) {
            } 
            }
            return true;
        }
}

另外,另一个问题是,例如,假设用户输入bob123作为他们的密码。

如何让循环告诉用户需要什么才能成为正确的密码?

在上面的示例中,它缺少大写字母和符号(*&amp; ^ ..等)。

如何在每次用户输入密码时将其添加到打印输出中,直到他们获得正确的密码来传递代码的所有规格?

5 个答案:

答案 0 :(得分:3)

你应该清楚地提到你的要求我不知道你的要求。请找到我的下面的解决方案`

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.print("Please enter a given  password : ");
    String passwordhere = in.nextLine();
    System.out.print("Please re-enter the password to confirm : ");
    String confirmhere = in.nextLine();

    List<String> errorList = new ArrayList<String>();

    while (!isValid(passwordhere, confirmhere, errorList)) {
        System.out.println("The password entered here  is invalid");
        for (String error : errorList) {
            System.out.println(error);
        }

        System.out.print("Please enter a given  password : ");
        passwordhere = in.nextLine();
        System.out.print("Please re-enter the password to confirm : ");
        confirmhere = in.nextLine();
    }
    System.out.println("your password is: " + passwordhere);

}

public static boolean isValid(String passwordhere, String confirmhere, List<String> errorList) {

    Pattern specailCharPatten = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
    Pattern UpperCasePatten = Pattern.compile("[A-Z ]");
    Pattern lowerCasePatten = Pattern.compile("[a-z ]");
    Pattern digitCasePatten = Pattern.compile("[0-9 ]");
    errorList.clear();

    boolean flag=true;

    if (!passwordhere.equals(confirmhere)) {
        errorList.add("password and confirm password does not match");
        flag=false;
    }
    if (passwordhere.length() < 8) {
        errorList.add("Password lenght must have alleast 8 character !!");
        flag=false;
    }
    if (!specailCharPatten.matcher(passwordhere).find()) {
        errorList.add("Password must have atleast one specail character !!");
        flag=false;
    }
    if (!UpperCasePatten.matcher(passwordhere).find()) {
        errorList.add("Password must have atleast one uppercase character !!");
        flag=false;
    }
    if (!lowerCasePatten.matcher(passwordhere).find()) {
        errorList.add("Password must have atleast one lowercase character !!");
        flag=false;
    }
    if (!digitCasePatten.matcher(passwordhere).find()) {
        errorList.add("Password must have atleast one digit character !!");
        flag=false;
    }

    return flag;

}

答案 1 :(得分:0)

  

我不确定为什么当我输出它时输出不会考虑特殊字符并引发错误。

提示:看一下这个片段:

    for (int p = 0; p < passwordhere.length(); p++) {
        if (Character.isUpperCase(passwordhere.charAt(p))) {
        }
    }

当它看到一个大写字符时它做了什么?

提示2:我认为你需要计算各种角色类中的角色然后....

  

如何让循环告诉用户需要什么才能成为正确的密码?对于上面的示例,它缺少大写字母和符号(*&amp; ^ .. etc)

提示:你的isValid方法需要&#34;告诉&#34;某人或某事为什么密码无效。想想它如何做到这一点。 (提示2:我可以想到三种不同的方法:异常,返回值,打印)

答案 2 :(得分:0)

使用此bean验证库进行密码验证:

https://github.com/ankurpathak/password-validation https://mvnrepository.com/artifact/com.github.ankurpathak.password/password-validation

它提供了许多约束来处理密码验证,还有更多 将在不久的将来添加:

  1. ContainDigit:验证密码是否包含指定位数。
  2. ContainLowercase:用于验证密码是否包含指定数量的小写字母。
  3. ContainSpecial:用于验证密码是否包含指定数量的特殊符号。
  4. ContainUppercase:验证密码是否包含指定数量的大写字母。
  5. NotContainWhitespace:要验证,不应有任何空格。
  6. PasswordMatches:用于验证密码和确认密码是否相等。你可以移动 还通过使用标志showErrorOnConfirmPassword移动约束以确认密码字段 (默认为true)。

默认情况下,所有约束都忽略空白,以便由NotBlank单独报告 标准bean验证约束,我们可以使用ignoreBlank(默认为true)转为相同约束 每个约束的标志。

使用该库的小例子是:


    @PasswordMatches
    public class PasswordDto {
     @Size(min = 8, max = 30)
     @NotContainWhitespace
     @ContainSpecial
     @ContainDigit
     private String password;
     @NotBlank
     private String confirmPassword;
    }

答案 3 :(得分:0)

import javax.swing.JOptionPane;

public class Validation {   

static String password;

public static boolean IsValidInput(String s) {

 boolean status = false;    
 char [] array = s.toCharArray();
 int lower=0, upper=0, digits=0;

 if (s.length() > 8) 
 status = true;

  for ( int i = 0;  i < array.length; i++) {
   if(Character.isDigit(array[i]))
      digits++;
   if(Character.isLowerCase(array[i]))
      lower++;
   if(Character.isUpperCase(array[i]))
      upper++;
 }

   if ( !(lower  > 0 ))
   status = false;

   if ( !(upper  > 0 ))
   status = false;

   if ( !(digits > 0 ))
   status = false;

   return status;
 }     

 public static void  setPassword(String p) {
 if (IsValidInput(p)) {
  password = p;
 JOptionPane.showMessageDialog( null, " Your Password is accepted -" + p);
 }

 else {
 password ="";
 JOptionPane.showMessageDialog( null, " Your  Password is  not accepted -" + p);
 }
 }

}

答案 4 :(得分:-1)

您好请检查以下代码,它可能会对您有所帮助

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.print("Please enter a given  password : ");
    String passwordhere = in.nextLine();
    System.out.print("Please re-enter the password to confirm : ");
    String confirmhere = in.nextLine();
    System.out.println("your password is: " + passwordhere);
    List<String> errorList=isValid(passwordhere,confirmhere);
    while (!errorList.isEmpty()) {
        System.out.println("The password entered here  is invalid");
        for(String error : errorList){
            System.out.println(error);
        }
        String Passwordhere = in.nextLine();
        System.out.print("Please re-enter the password to confirm : ");

    }

}

public static List<String> isValid(String passwordhere, String confirmhere) {

    List<String> errorList = new ArrayList<String>();

    Pattern specailCharPatten = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
    Pattern UpperCasePatten = Pattern.compile("[A-Z ]");
    Pattern lowerCasePatten = Pattern.compile("[a-z ]");
    Pattern digitCasePatten = Pattern.compile("[0-9 ]");

    if (!passwordhere.equals(confirmhere)) {
        errorList.add("password and confirm password does not match");
    }
    if (passwordhere.length() <= 8) {
        errorList.add("Password lenght must have alleast 8 character !!");
    }
    if (!specailCharPatten.matcher(passwordhere).find()) {
        errorList.add("Password must have atleast one specail character !!");
    }
    if (!UpperCasePatten.matcher(passwordhere).find()) {
        errorList.add("Password must have atleast one uppercase character !!");
    }
    if (!lowerCasePatten.matcher(passwordhere).find()) {
        errorList.add("Password must have atleast one lowercase character !!");
    }
    if (!digitCasePatten.matcher(passwordhere).find()) {
        errorList.add("Password must have atleast one digit character !!");
    }

    return errorList;

}