支持正序反序
支持数字和大小写字母
ps:该方法提供了俩个参数,分别校验数字个数和字母个数,若有其他需求可做删除修改,功能不变
public static VerifyPasswordType hasContinuousChars(String str, int x, int y) {
VerifyPasswordType passwordType = new VerifyPasswordType();
passwordType.setState(false);
if (str == null || str.length() < x || str.length() < y) {
return passwordType;
}
char[] arr = str.toLowerCase().toCharArray();
int count = 1;
int lastCode = arr[0];
for (int i = 1; i < arr.length; i++) {
int nowCode = arr[i];
if (nowCode >= '0' && nowCode <= '9' || nowCode >= 'a' && nowCode <= 'z') {
int countType;
String stateType;
if(nowCode >= '0' && nowCode <= '9'){
if (x <= 1){
continue;
} else {
countType = x;
stateType = "数字";
}
}else {
if (y <= 1){
continue;
} else {
countType = y;
stateType = "字母";
}
}
// 正序或反序校验
if (nowCode == lastCode + 1 || nowCode == lastCode - 1) {
count++;
if (count >= countType) {
// 判断是否出现aba或bab情况
if (i > 1 && nowCode == arr[i - 2]) {
count = 2;
} else {
passwordType.setType(stateType);
passwordType.setState(true);
StringBuilder sb = new StringBuilder();
for (int j = 0; j < count; j++) {
sb.insert(0, arr[i - j]);
}
passwordType.setMessage(sb.toString());
return passwordType;
}
}
} else {
count = 1;
}
lastCode = nowCode;
} else {
count = 1;
lastCode = 0;
}
}
return passwordType;
}
VerifyPasswordType类:
public class VerifyPasswordType {
private Boolean state;
private String type;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Boolean getState() {
return state;
}
public void setState(Boolean state) {
this.state = state;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}