密码验证 8 位数字,包含大写、小写和特殊字符

Posted

技术标签:

【中文标题】密码验证 8 位数字,包含大写、小写和特殊字符【英文标题】:Password validate 8 digits, contains upper, lowercase, and a special character 【发布时间】:2016-07-05 23:00:03 【问题描述】:

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

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 作为他们的密码。

我怎样才能让循环告诉用户它需要一个正确的密码?

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

如何在用户每次输入密码时添加它以打印出来,直到他们获得正确的密码以通过代码的所有规范?

【问题讨论】:

@Programminnoob 请对使用正则表达式的密码验证进行一些研究,然后告诉我您发现了什么。 Character.isSpecialCharacter 不是函数,检查特殊字符的最简单方法是使用正则表达式,因此您不妨学习如何正确执行此操作 请参考***.com/a/12885952/2308683 @cricket_007 我怎样才能将其中的任何一个隐含到我的代码中。 ' 返回 (s == null) ? false : s.matches("[^A-Za-z0-9 ]");'也许这条线? 替换你不存在的Character.isSpecialCharacter 我是否将其用作单独的方法?还是我让它成为一个循环? @cricket_007 【参考方案1】:
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);
     
     

    

【讨论】:

【参考方案2】:

使用这个 bean 验证库进行密码验证:

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

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

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

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

使用该库的小例子是:

@PasswordMatches 公共类密码Dto @Size(最小值 = 8,最大值 = 30) @NotContainWhitespace @ContainSpecial @ContainDigit 私人字符串密码; @NotBlank 私人字符串确认密码;

【讨论】:

【参考方案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;


【讨论】:

【参考方案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);
    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;


【讨论】:

此代码有效,但如果我有一个像 Bob123 这样的密码并且它没有特殊字符,它会告诉我我需要一个特殊字符,但它不会让我重新启动密码部分。我该如何解决它以重新启动第一个问题并让用户输入新密码?【参考方案5】:

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

提示:看看这个片段:

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

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

提示 2:我认为您需要计算各种字符类中的字符,然后......

我怎样才能让循环告诉用户它需要一个正确的密码?对于上面的示例,它缺少一个大写字母和一个符号(*&^..etc)

提示:您的 isValid 方法需要“告诉”某人或某事为什么密码无效。想想它是如何做到的。 (提示 2:我可以想到三种不同的方法:异常、返回值、打印)

【讨论】:

它循环查看密码是否有大写字母..? @Programminnoob 是的,但是有一个大写字母时会发生什么? if语句里面是什么? 它告诉布尔值返回true? :o @cricket_007 对于斯蒂芬斯评论的第二部分,我是否应该在循环内添加一个打印语句,例如:'for (int p = 0; p @Programminnoob 在空的 if 语句中没有返回任何内容。您正在循环并正确检查大写字母,但没有做任何事情,因为 if 语句的主体是空的并且没有 else 语句(提示:非大写字母是小写字母......您不需要两个循环)

以上是关于密码验证 8 位数字,包含大写、小写和特殊字符的主要内容,如果未能解决你的问题,请参考以下文章

随机生成密码,长度6-10位、不可包含特殊字符、必须包含大写、小写和数字,oracle 如何实现?

正则表达式验证密码必须由大小写字母、数字、特殊字符组成

8-16位密码,数字、大小写字母组合、符号至少包含两种,是啥意思?

前端密码校验8位以上,包含大写字母小写字母数字特殊符号中的 3 种以上

js密码正则表达式:要求包含大小写字母、数字和特殊符号,8~16位

js密码正则表达式:要求包含大小写字母、数字和特殊符号,8~16位