简化 java 正则表达式代码以验证密码

Posted

技术标签:

【中文标题】简化 java 正则表达式代码以验证密码【英文标题】:Simplify java regex code to validate passwords 【发布时间】:2021-11-08 11:08:54 【问题描述】:

我想告诉用户他需要什么,这样他的密码才有效,当密码不符合要求时,jlabels 将颜色变为红色,当满足时变为黑色。我可以做我想做的最短的方法,但它会存在其他吗?我附上代码和照片

    private void JB_MostrarActionPerformed(java.awt.event.ActionEvent evt)                                            

    String password = "";

    password = jPasswordField_pass.getText().trim();

    final String regex = "^(?:(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=.,]).*)[^\\s]8,$";

    if (password.equals("")) 
        jl_Error.setText("Please enter your Password.");
        jl_Error.setForeground(Color.red);

     else if (password.matches(regex) == false) 

        jLabel_infopass.setText("Your password must be between 8 and 64 characters long. It must contain at least three (3) of the below:");
        jLabel_infopass.setForeground(Color.red);

        if (Pattern.matches("(?=.*\\d).*", password) == false) 
            jLabel_infopass3.setText("numbers (0-9)");
            jLabel_infopass3.setForeground(Color.red);
         else 
            jLabel_infopass3.setText("numbers (0-9)");
            jLabel_infopass3.setForeground(Color.black);
        
        if (Pattern.matches("(?=.*[a-z]).*", password) == false) 
            jLabel_infopass2.setText("lowercase (a-z)");
            jLabel_infopass2.setForeground(Color.red);
         else 
            jLabel_infopass2.setText("lowercase (a-z)");
            jLabel_infopass2.setForeground(Color.black);
        
        if (Pattern.matches("(?=.*[A-Z]).*", password) == false) 
            jLabel_infopass1.setText("uppercase (A-Z)");
            jLabel_infopass1.setForeground(Color.red);
         else 
            jLabel_infopass1.setText("uppercase (A-Z)");
            jLabel_infopass1.setForeground(Color.black);
        
        if (Pattern.matches("(?=.*[@#$%^&+!.,]).*", password) == false) 
            jLabel_infopass4.setText("symbols (e.g. #, $, !, @, ^, &, *, etc)");
            jLabel_infopass4.setForeground(Color.red);
         else 
            jLabel_infopass4.setText("symbols (e.g. #, $, !, @, ^, &, *, etc)");
            jLabel_infopass4.setForeground(Color.black);
        

     else 
        jLabel_infopass.setText("Your password must be between 8 and 64 characters long. It must contain at least three (3) of the below:");
        jLabel_infopass.setForeground(Color.black);
        JOptionPane.showMessageDialog(null, password);
    
 

【问题讨论】:

扔掉四台显示器,把规则告诉他一个。然后只需使用一个匹配或不匹配的正则表达式。这是矫枉过正。 【参考方案1】:

    使用简单的一行来告知用户密码限制。单行显示“您的密码长度必须为 8-64 个字符,并且至少包含三个数字或特殊字符”

    使用 Pattern 类来验证您的密码。

    import java.util.regex.*;  
    
    // this will check for 8-64 character with at least 3 numbers or special characters
    String passwordRegex = "(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$@!%&*?])[A-Za-z\d#$@!%&*?]8,64[\d#$@!%&*?]3,";
    
    Pattern pattern = Pattern.compile(passwordRegex);
    
    // this is inside your password check function
    Matcher match = pattern.matcher(passwordCheck);
    
    if(match.matches())
    // OK
     else 
    // show warning message
    
    

【讨论】:

以上是关于简化 java 正则表达式代码以验证密码的主要内容,如果未能解决你的问题,请参考以下文章

JS的常用正则表达式 验证密码用户名等

JS的常用正则表达式 验证密码用户名等

用于密码验证的正则表达式 Java

JAVA正则验证[密码]。验证规则:[5-20位字符,英文,数字或各种符号,不能存在单一形式]。 答案如下:

用于密码验证的正则表达式

用于密码验证的正则表达式 [重复]