import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 密码验证
* @author cec
*
*/
public class PassWordUtils {
/**
* 密码验证
* @Description
*/
public static String validatePwd(String pwd,String loginName){
String errorInfo ="";
//密码至少包含大写字母、小写字母、数字、特殊符号中的三种,长度至少8位
//String regex1 = "^(?=^.{8,}$)((?=.*\\d)|(?=.*\\W+))(?![.\\n])(?=.*[A-Z])(?=.*[a-z]).*$";
String regex1 = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{8,}$";
Pattern regex = Pattern.compile(regex1);
Matcher matcher = regex.matcher(pwd);
boolean flag = matcher.matches();
if(flag){
pwd = pwd.toLowerCase();//转成小写
loginName = loginName.toLowerCase();//转成小写
if(pwd.indexOf(loginName)!=-1||loginName.indexOf(pwd)!=-1){
errorInfo = "密码不能与登录名相似";
return errorInfo;
}
}else{
errorInfo = "密码至少包含大写字母、小写字母、数字、特殊符号中的三种,长度至少8位";
return errorInfo;
}
boolean b = keyValiation(pwd);
if(b){
errorInfo = "密码不能按键盘的排序";
}
return errorInfo;
}
public static boolean keyValiation(String pwd) {
boolean isTrue = false;
char[][] keys = { { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' },
{ 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p' },
{ 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';' },
{ 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/' } };
char[] pwdChars = pwd.toLowerCase().toCharArray();
List list = new ArrayList();
int strIndex = 0;
for (int k = 0; k < pwdChars.length; k++) {
for (int i = 0; i < keys.length; i++) {
for (int j = 0; j < keys[i].length; j++) {
if (keys[i][j] == pwdChars[k]) {
list.add(i + "," + j);
//System.out.print(i+","+j+" ");
}
}
}
}
int index = 1;
int tmpY = Integer.parseInt(((String) list.get(0)).split(",")[0]);
int tmpX = Integer.parseInt(((String) list.get(0)).split(",")[1]);
for (int i = 1; i < list.size(); i++) {
int y = Integer.parseInt(((String) list.get(i)).split(",")[0]);
int x = Integer.parseInt(((String) list.get(i)).split(",")[1]);
if (tmpY == y) {
if (tmpX - x == -1) {
tmpX = x;
index++;
if (index > 2) {
break;
}
continue;
} else {
tmpY = y;
tmpX = x;
index = 1;
continue;
}
} else {
index = 1;
tmpX = x;
tmpY = y;
}
}
if (index < 3) {
tmpY = Integer.parseInt(((String) list.get(0)).split(",")[0]);
tmpX = Integer.parseInt(((String) list.get(0)).split(",")[1]);
for (int i = 1; i < list.size(); i++) {
int y = Integer.parseInt(((String) list.get(i)).split(",")[0]);
int x = Integer.parseInt(((String) list.get(i)).split(",")[1]);
/*
* System.out.println("x="+x+" "+"y="+y);
* System.out.println("tmpX="+tmpX+" "+"tmpY="+tmpY);
*/
if (tmpY == y) {
if (tmpX - x == 1) {
tmpX = x;
index++;
if (index > 2) {
break;
}
continue;
} else {
tmpY = y;
tmpX = x;
index = 1;
continue;
}
} else {
index = 1;
tmpX = x;
tmpY = y;
}
}
}
//System.out.println(index);
if (index > 2) {
isTrue = true;
}
return isTrue;
}
public static void main(String[] args) {
PassWordUtils p = new PassWordUtils();
String str = p.validatePwd("89**1qwe","test3");
System.out.println(str);
}
}