zoukankan      html  css  js  c++  java
  • javascript原生 实现数字字母混合验证码

    实现4位数 数字字母混合验证码(数字+大写字母+小写字母)

    ASCII 字符集中得到3个范围:

      1. 48-57 表示数字0-9 

      2. 65-90 表示大写字母

      3. 97-122 表示小写字母

    范围随机数:
        parseInt(Math.random()*4);  //此时Math.random意为“大于等于0,小于4”,取整后为0~3
        // 如何把最小值变成1呢?   +1就变成了1
        parseInt(Math.random()*4)+1;  // +1后范围变成了“大于等于1,小于5”,parseInt取整后范围是1~4
    
        // 如果想获取50-100的范围随机数  min ~ max
        min + parseInt(Math.random()*(max - min + 1)); //+1后Math.random取值为 大于等于0,小于51
        // min + 0 ~ 50   = 50~100

    具体实现如下:

     1     <script>
     2         // 获取随机数的范围
     3         function getRandomInt (min , max){
     4             return min + parseInt(Math.random() * (max - min + 1))
     5         }
     6         // 获取验证码
     7         function getStringValidate(){
     8             var res = "";
     9             var min , max;
    10             // 循坏代码进行拼接
    11             for(var i = 0; i < 4; i++){
    12                 // 选择范围 1,2,3
    13                 // 决定 范围的开头和结尾
    14                 switch(getRandomInt(1 , 3)){
    15                     // 1是数字
    16                     case 1 :
    17                         min = 48; max = 57; 
    18                         break;
    19                     // 2是大写字母
    20                     case 2 :
    21                         min = 65; max = 90; 
    22                         break;
    23                     // 3是小写字母
    24                     case 3 :
    25                         min = 97; max = 122; 
    26                         break;
    27                 }
    28                 // console.log(min , max);//随机范围
    29                 var randomInt = getRandomInt(min , max);
    30                 // var randomString = String.fromCharCode(randomInt);
    31                 // console.log(randomString);//一个随机字符  经过上面的4次循环,生成4个随机数
    32                 // 拼接起来
    33                 res += String.fromCharCode(randomInt);
    34             }
    35             // console.log(randomString);//经过上面的4次循环,生成4个随机数
    36             console.log(res);
    37             return res;
    38         }
    39         getStringValidate();
    40     </script>
  • 相关阅读:
    mysql批量插入数据的基类
    mount命令解析
    常用linux命令记录
    转载一篇大神的博客文章
    linux查看网卡状态
    centos7配置网卡绑定
    coentos7安装python3
    阿里云ecs 硬盘在线扩容
    centos7安装redis5
    centos7 rpm安装nginx
  • 原文地址:https://www.cnblogs.com/uuind/p/12672925.html
Copyright © 2011-2022 走看看