通过asc码的方式校验用户密码是否连续

支持正序反序

支持数字和大小写字母

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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值