JS四位随机验证码

// 随机验证码4个

// 0~9
// a~z
// A~Z

// random(0,9);    随机数字
// String.fromCharCode(random(97,122));    随机小写
// String.fromCharCode(random(65,90));     随机大写

// 1. 产生一个随机字符库,至少是12个,最好是3和4的公倍数个
   
    <script>
        var str ="";
        for(var i=1;i<4;i++){
     
     
        // 从每个中取出四个随机数
        str +=random(0,9);
        // 数字随机数
        str += String.fromCharCode(random(97,122))
        // 随机数大小写
        str += String.fromCharCode(random(65,90)) 
        }
console.log(str);

        var randomStr="";
        for(var i=0;i<4;i++){
     
     
            randomStr += str[random(0,str.length-1)];
                // str[里面随机出四个数是str的下标组合起来]
        }
        console.log(randomStr);


        // 通用范围随机函数的功能
function random(max,min){
     
     
     return Math.round(Math.random()*(max-min)+min);
}
    </script>

猜你喜欢

转载自blog.csdn.net/qq_26705343/article/details/111648997
今日推荐