Javascript中验证码的生成(随机数字+大小写字母)

Javascript中验证码的生成(随机数字+大小写字母)

思路:利用Math的random函数,随机生成arr数组的下标即可。
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>验证码</title>
    <script>
        //产生[start,end)的随机数
        //PS:若想end为闭区间,则可以return parseInt(Math.random()*(end-start+1)+start)
        function rand(start,end){
            return parseInt(Math.random()*(end-start)+start);
        }
        function vercode(){
            var arr = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
                "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
                0,1,2,3,4,5,6,7,8,9];
            var str = "";
            //alert(arr.length);
            var a = rand(0,63);
            str += arr[a];
            var b = rand(0,63);
            str += arr[b];
            var c = rand(0,63);
            str += arr[c];
            var d = rand(0,63);
            str += arr[d];
            var e = rand(0,63);
            str += arr[e];
            return str;
        }
    </script>
</head>
<body>
<script>
    alert("验证码是:"+vercode());
</script>
   <input type="button" onclick="cal()">
</body>
</html>
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页