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

效果图:

相关推荐
前端 贾公子19 小时前
v-if 与 v-for 的优先级对比
开发语言·前端·javascript
嗯嗯=20 小时前
python学习篇
开发语言·python·学习
不会c嘎嘎1 天前
QT中的常用控件 (二)
开发语言·qt
小二·1 天前
Pinia 完全指南:用 TypeScript 构建可维护、可测试、可持久化的 Vue 3 状态管理
javascript·vue.js·typescript
是一个Bug1 天前
50道核心JVM面试题
java·开发语言·面试
bug总结1 天前
Vue3 实现后台管理系统跳转大屏自动登录功能
前端·javascript·vue.js
用户47949283569151 天前
同事一个比喻,让我搞懂了Docker和k8s的核心概念
前端·后端
烛阴1 天前
C# 正则表达式(5):前瞻/后顾(Lookaround)——零宽断言做“条件校验”和“精确提取”
前端·正则表达式·c#
她和夏天一样热1 天前
【观后感】Java线程池实现原理及其在美团业务中的实践
java·开发语言·jvm
C_心欲无痕1 天前
浏览器缓存: IndexDB
前端·数据库·缓存·oracle