获取随机字符构成的,指定长度的字符串,形成验证码
//获取指定范围内的随机证整数
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function getRandomStr(length) {
var characterDic =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
//接收
let newStr = '';
for (let i = 0; i < length; i++) {
//产生一个0-64之间的随机下标
let index = getRandomNumber(0, 64);
//产生一个随机字符
let tempStr = characterDic[index];
newStr += tempStr;
}
return newStr;
}
let str = getRandomStr(5);
console.log(str);