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

效果图:

相关推荐
Csvn4 小时前
OpenSpec 详细使用教程
前端
方也_arkling5 小时前
【Java-Day08】static / final / 枚举
java·开发语言
风吹夏回5 小时前
Python 全局异常处理:从“满屏 try-except”到优雅兜底
开发语言·python
Chengbei115 小时前
一站式源码安全检测工具、云安全 / APP / 小程序源码敏感信息递归多层目录扫描AK、JWT、手机号、身份证等敏感信息
java·开发语言·安全·web安全·网络安全·系统安全·安全架构
llz_1125 小时前
web-第一次课后作业
java·开发语言·idea
小熊Coding5 小时前
Python爬取当当网二手图书项目实战!
开发语言·爬虫·python·beautifulsoup·requests·二手图书
之歆5 小时前
Day19_LESS 完全指南——从入门到工程实践
前端·css·less
秋95 小时前
Java项目运行5天左右自动宕机:系统性定位与解决方案
java·开发语言·python
xiaoshuaishuai86 小时前
C# 内存管理与资源泄漏
开发语言·c#
云水一下6 小时前
HTML5 从入门到精通:实战收官——从零搭建完整静态网站,综合运用所有知识
前端·html5