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());

效果图:

相关推荐
乐观勇敢坚强的老彭18 分钟前
C++信奥:开关门、开关灯问题
开发语言·c++·算法
冻柠檬飞冰走茶18 分钟前
PTA基础编程题目集 7-31 字符串循环左移(C语言实现)
c语言·开发语言·数据结构·算法
卷福同学1 小时前
AI编程出海第二步:验证关键词能否做站
前端·人工智能·后端
赵庆明老师1 小时前
Vben精讲:21-详解web-antd:tsconfig.json
前端·json·vim
a1117761 小时前
中文优先的企业 RAG 知识库 开源项目
开发语言·开源·kotlin
wc881 小时前
微软EDGE浏览器功能学习
前端·学习·edge
破z晓1 小时前
javascript 导出excel表
开发语言·javascript·excel
t-think2 小时前
C++类和对象详解(一)
开发语言·c++
西门啐血2 小时前
上位机开发之假装有设备,使用 C# 模拟串口设备
开发语言·mongodb·c#
Csvn2 小时前
🎯 原生 `<dialog>` 元素:终于可以扔掉一半的自定义弹窗组件了?
前端