js中随机生成4位数的验证码,要有字母和数字

代码:

javascript 复制代码
function code(){
            let num1 = Math.floor(Math.random()*10)
            let num2 = Math.floor(Math.random()*10)
            let letter1 = String.fromCharCode(Math.floor(Math.random() * 26) + 97);
            let letter2 = String.fromCharCode(Math.floor(Math.random() * 26) + 65);
            var arr = [num1,num2,letter1,letter2]
            var str = "";
            for(var i=0;i<4;i++){
                str += arr[Math.floor(Math.random()*(arr.length))]
            }
            return str
        }
        document.write(code());

效果图:

相关推荐
忆锦紫7 分钟前
图像增强算法:Gamma映射算法及MATLAB实现
开发语言·算法·matlab
qq_4198540530 分钟前
CSS动效
前端·javascript·css
烛阴32 分钟前
3D字体TextGeometry
前端·webgl·three.js
桜吹雪1 小时前
markstream-vue实战踩坑笔记
前端
南村群童欺我老无力.1 小时前
Flutter应用鸿蒙迁移实战:性能优化与渐进式迁移指南
javascript·flutter·ci/cd·华为·性能优化·typescript·harmonyos
亲爱的非洲野猪1 小时前
Java锁机制八股文
java·开发语言
C_心欲无痕1 小时前
nginx - 实现域名跳转的几种方式
运维·前端·nginx
花哥码天下1 小时前
恢复网站console.log的脚本
前端·javascript·vue.js
LawrenceLan1 小时前
Flutter 零基础入门(十二):枚举(enum)与状态管理的第一步
开发语言·前端·flutter·dart
Zoey的笔记本2 小时前
「支持ISO27001的GTD协作平台」数据生命周期管理方案与加密通信协议
java·前端·数据库