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

效果图:

相关推荐
寻道码路6 分钟前
LangChain4j Java AI 应用开发实战(二十六):多模型集成策略 —— OpenAI、DeepSeek、阿里百炼混合使用
java·开发语言·人工智能·ai
面朝大海,春不暖,花不开12 分钟前
BPF与eBPF简介:核心概念与观测工具概览
开发语言·php·ebpf·bpf·性能观测
ch.ju13 分钟前
Java Programming Chapter 4——Static code block
java·开发语言
risc12345615 分钟前
Lucene80DocValuesConsumer 五种类型源码阅读顺序
java·服务器·前端
弹简特15 分钟前
【Java项目-企悦抽】04-项目演示+项目源码+AI赋能整理接口文档
java·开发语言
郝学胜-神的一滴17 分钟前
Qt 高级编程 034:深耕QWidget底层内核—彻底吃透无边框窗口设计核心原理
开发语言·c++·qt·程序人生·软件开发·用户界面
小米渣的逆袭22 分钟前
Chrome Extension Script World(ISOLATED / MAIN)原理与适用场景
前端·javascript·chrome
不会写代码的ys27 分钟前
C++复习篇
java·开发语言·c++
雨师@32 分钟前
go语言项目--实例化(图书管理)--005
开发语言·后端·golang
微信开发api-视频号协议37 分钟前
Codex++安全边界探秘:从模型能力到风险防御
前端·安全·微信·企业微信