密码校验 长度8位数字和字母混合+密码生成

版权声明:作者支付宝18696232390喜欢的可以打钱! https://blog.csdn.net/u014131617/article/details/88355762

密码生成

public class PwdGenderUtils {

    private static Integer length = 8; //生成的密码长度

    //生成随机数字和字母,
    public static String getStringRandom() {

        String val = "";
        Random random = new Random();
        //length为几位密码
        for(int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char)(random.nextInt(26) + temp);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }


    public static Integer getLength() {
        return length;
    }

    public static void setLength(Integer length) {
        PwdGenderUtils.length = length;
    }
}

密码校验

    function checkPwd(){
        var pwd = document.getElementById("pwdId").value;
        if(pwd.length<8){
            layer.msg('密码长度最低8位', {icon: 2});
        }
        var regNumber = /\d+/; //验证0-9的任意数字最少出现1次。
        var regString = /[a-zA-Z]+/; //验证大小写26个字母任意字母最少出现1次。

        //验证第一个字符串
        if (regNumber.test(pwd) && regString.test(pwd)) {
            return true
        }else{
            layer.msg('密码里面最少存在一个字母或数字', {icon: 2});
            return false
        }

        return false
    }

猜你喜欢

转载自blog.csdn.net/u014131617/article/details/88355762