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

效果图:

相关推荐
程序员黑豆1 分钟前
鸿蒙应用开发:Flex 组件从入门到实战
前端·华为·harmonyos
大爱编程♡1 分钟前
Vue3+TypeScript+element-plus的文章管理项目(配后端接口)-退出及文章管理页面
前端·javascript·typescript
先吃饱再说3 分钟前
React 组件设计:从 Props 传递到组件封装
前端·react.js
独立开阀者_FwtCoder6 分钟前
最近做了一个健身小程序:智形健身助手,健身的佬们来提点意见
前端·javascript·github
舒灿9 分钟前
喵ing Tab 1.0.0 正式发布
前端·ai编程
葡萄城技术团队13 分钟前
当表格数据来自外部系统:SpreadJS 如何让异步公式一键刷新
前端
小徐_233317 分钟前
Open Wot 1.0.5 发布:让 AI 接入 wot-ui,只需要两条命令
前端·uni-app·ai编程
Android研究员21 分钟前
Android进阶之事件分发机制深度剖析
android·前端·面试
Csvn28 分钟前
🛡️ 前端水印方案全解析——从「能被 F12 删掉」到「删不掉」的反破解实战
前端
Lyndon-李大鹏1 小时前
QT-VS环境
开发语言·qt