随机生成4位验证码,大写字母小写字母及数字可供随机选择(比如:nG3p)
var arr = []; for (var i = 48; i <= 57; i++) { arr.push(String.fromCharCode(i)); } console.log(arr); for (var j = 65; j <= 90; j++) { arr.push(String.fromCharCode(j)) } console.log(arr); for (var k = 97; k <= 122; k++) { arr.push(String.fromCharCode(k)) } console.log(arr); var str = ''; for (var i = 0; i < 4; i++) { var index = Math.floor(Math.random() * arr.length); //index代表下标。 //利用下标找下标对应的值 console.log(index); str += arr[index] } console.log(str);