密码验证正则表达式android

时间:2016-11-28 08:02:03

标签: java android regex string

我需要进行以下密码验证:

  1. 至少最小字符数8和最大字符数15
  2. 至少一个号码和一个特殊字符来自(!@#$%^& * - = +?。);
  3. 至少一封小写字母
  4. 密码不应是用户名和电子邮件的子字符串(最小长度3和最大长度15)。
  5. 密码应区分大小写。
  6. 我也看过这些answers,但我很困惑,我应该使用输入过滤器来实现这个还是Regex?

    任何帮助都会很明显。如果你提供一个有效的解决方案,那就太棒了。

3 个答案:

答案 0 :(得分:3)

public class Validation {

    public static void main(String[] args) {  
       String pass = "1AB%CDef555";
       String username = "manna";
       String email = "mannx@rtt.com";
       System.out.println(validiate2(pass, username,email));
    }
    // if you don't care why it fails and only want to know if valid or not 
    public static boolean validiate (String pass, String username, String email){
        String pattern = "^(?=.*[0-9])(?=.*[a-z])(?=.*[!@#$%^&*+=?-]).{8,15}$";
        if(pass.matches(pattern)){
            for(int i=0;(i+3)<username.length();i++){
                if(pass.contains(username.substring(i,i+3)) || username.length()<3 || username.length()>15){
                   return false; 
                }                  
            }
            for(int i=0;(i+3)<email.length();i++){
                if(pass.contains(email.substring(i,i+3)) || email.length()<3 || email.length()>15){
                   return false; 
                }                  
            }
            return true;
        }
        return false;    
    }
    // if you want to know which requirement was not met
    public static boolean validiate2 (String pass, String username, String email){
        if (pass.length() < 8 || pass.length() >15 ){
            System.out.println("pass too short or too long");
            return false;
        }
        if (username.length() < 3 || username.length() >15 ){
            System.out.println("username too short or too long");
            return false;
        }
        if (!pass.matches(".*\\d.*")){
            System.out.println("no digits found");
            return false;
        } 

        if (!pass.matches(".*[a-z].*")) {
            System.out.println("no lowercase letters found");
            return false; 
        }
        if (!pass.matches(".*[!@#$%^&*+=?-].*")) {
            System.out.println("no special chars found");
            return false; 
        } 
        if (containsPartOf(pass,username)) {
            System.out.println("pass contains substring of username");
            return false; 
        }
        if (containsPartOf(pass,email)) {
            System.out.println("pass contains substring of email");
            return false; 
        }
        return true;           
    }

    private static boolean containsPartOf(String pass, String username) {
        int requiredMin = 3
        for(int i=0;(i+requiredMin)<username.length();i++){
            if(pass.contains(username.substring(i,i+requiredMin))){
               return true; 
            }                  
        }
        return false;        
    }    
}

答案 1 :(得分:0)

你可以尝试这个:

^(?!.*(user|emailaddress))(?=.*\d)(?=.*[! @#$%^&*=+?.-])(?=.*[a-z]).{8,15}$

确保用变量替换用户和emailaddress

Explanation

此代码适用于我:

public class NewClass1 {
    public static void main(String[] args) {
        NewClass1 nc = new NewClass1();
        nc.check("abcd123-", "userName", "abc@yahoo.com");
        nc.check("userName1-", "userName", "abc@y.c");
        nc.check("abc@y.c1b", "userName", "abc@y.c");
        nc.check("abcy.c1b", "userName", "abc@y.c");
        nc.check("abcd123-", "userName", "abc@yahoo.com");
    }

    public void check(String string, String userName, String email) {

        final String regex = "^(?!.*(" + userName + "|" + email + "))(?=.*\\d)(?=.*[! @#$%^&*=+?.-])(?=.*[a-z]).{8,15}$";
        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);
        if (matcher.find()) {
            System.out.println(string + "Full match: " + matcher.group(0));
        } else {
            System.out.println("no match");
        }
    }
}

答案 2 :(得分:0)

对此有great library 它使用场地的anotations并且具有丰富的习惯。 我认为#4仍然需要手工完成,但你应该明确检查库。

以下是github的例子:

@Password(min = 6, scheme = Password.Scheme.ALPHA_NUMERIC_MIXED_CASE_SYMBOLS)
private EditText passwordEditText;

干杯。