java正则校验必须包括字母、数字和字符(6-12位)

java正则校验,必须包括字母数字字符

public static String checkPassword(String passwordStr) {
    if (passwordStr != null && !"".equals(passwordStr) && (passwordStr.length() < 6 || passwordStr.length() > 12)) {
        return "密码为 6-12 位字母、数字或英文字符!";
    }

    // Z = 字母       S = 数字           T = 特殊字符
    String regexZ = "[A-Za-z]+";
    String regexS = "^\\d+$";
    String regexT = "[~!@#$%^&*.]+";
    String regexZT = "[a-zA-Z~!@#$%^&*.]+";
    String regexZS = "[0-9A-Za-z]+";
    String regexST = "[\\d~!@#$%^&*.]*";
    String regexZST = "[\\da-zA-Z~!@#$%^&*.]+";
 
    if (passwordStr.matches(regexZ)){
        return "纯字母,弱";
    }
    if (passwordStr.matches(regexS)){
        return "纯数字,弱";
    }
    if (passwordStr.matches(regexT)){
        return "纯字符,弱";
    }
    if (passwordStr.matches(regexZT)){
        return "字母字符,中";
    }
    if (passwordStr.matches(regexZS)){
        return "字母数字,中";
    }
    if (passwordStr.matches(regexST)){
        return "数字字符,中";
    }
    if (passwordStr.matches(regexZST)) {
        return "强";
    }
    return "不知道是啥";
}

测试:

public static void main(String[] args)
{
    System.out.println(checkPassword("qqqqqq"));
    System.out.println(checkPassword("111111"));
    System.out.println(checkPassword("......"));
    
    System.out.println(checkPassword("qqq..."));
    System.out.println(checkPassword("111qqq"));
    System.out.println(checkPassword("111..."));
    
    System.out.println(checkPassword("11qq.."));
    System.out.println(checkPassword("11..qq"));
    System.out.println(checkPassword("qq..11"));
    System.out.println(checkPassword("qq11.."));
    System.out.println(checkPassword("..11qq"));
    System.out.println(checkPassword("..qq11"));
}

输出:

java正则校验必须包括字母、数字和字符(6-12位)_第1张图片

js正则:

if (!(/^(?=.*\d)(?=.*[a-zA-Z])(?=.*[~!@#$%^&*.])[\da-zA-Z~!@#$%^&*.]$/.test(newPassword))) {
	alert("密码必须包括字母、数字和英文字符!");
	return;
}

 

你可能感兴趣的:(java)