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

效果图:

相关推荐
小玮看世界3 分钟前
[Python] str() 和 join() 的区别与实战避坑指南
前端·javascript·python
特立独行的猫a6 分钟前
Tauri v2的Rust应用 → HarmonyOS(鸿蒙 PC)移植30分钟速成指南
开发语言·rust·harmonyos·tauri·移植·鸿蒙pc
hyf3266338 分钟前
低成本站群泛程序部署:单服务器托管百站稳定运行教程
运维·服务器·前端·搜索引擎·蜘蛛池
迷迭香yy10 分钟前
大宗交易折溢价因子怎么挖掘本地化Python全流程实战
开发语言·人工智能·python
xieliyu.15 分钟前
UPD协议结构以及开发中注意事项
java·开发语言·笔记·java-ee
Yweir19 分钟前
AI大模型开发-Python介绍、版本说明
开发语言·人工智能·python
程序员黑豆24 分钟前
Java正则表达式详解
java·前端·ai编程
峥嵘life8 小时前
Android16 311Y3 EAP-TLS 网络连接失败分析与修复总结
android·开发语言·人工智能·php
不可求~9 小时前
多模型路由怎么落地:把任务分流写进项目的最小实现
java·前端·数据库
mqiqe11 小时前
AgentScope Java Harness:4. 双层记忆系统 让 Agent 拥有真正的“长期大脑“
java·开发语言