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

效果图:

相关推荐
Cyclo-27 分钟前
PDFJS 在React中的引入 使用组件打开文件流PDF
前端·react.js·pdf
智商低情商凑3 小时前
Go学习之 - Goroutines和channels
开发语言·学习·golang
半桶水专家3 小时前
Go 语言时间处理(time 包)详解
开发语言·后端·golang
编程点滴3 小时前
Go 重试机制终极指南:基于 go-retry 打造可靠容错系统
开发语言·后端·golang
椒盐螺丝钉3 小时前
Vue Router应用:组件跳转
前端·javascript·vue.js
顾安r3 小时前
11.20 开源APP
服务器·前端·javascript·python·css3
实心儿儿3 小时前
C++ —— 模板进阶
开发语言·c++
敲敲了个代码3 小时前
CSS 像素≠物理像素:0.5px 效果的核心密码是什么?
前端·javascript·css·学习·面试
萧鼎3 小时前
Python PyTesseract OCR :从基础到项目实战
开发语言·python·ocr
少云清4 小时前
【软件测试】5_基础知识 _CSS
前端·css·tensorflow