var code;
function createCode() {
code = "";
var codeLength = 6; //组成验证码位数
var selectChar = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C',
'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c',
'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
for (var i = 0; i < codeLength; i++) {
var charIndex = Math.floor(Math.random() * 60);
code += selectChar[charIndex];
}
return code;
}
function createCode() {
code = "";
var codeLength = 6; //组成验证码位数
var selectChar = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C',
'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c',
'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
for (var i = 0; i < codeLength; i++) {
var charIndex = Math.floor(Math.random() * 60);
code += selectChar[charIndex];
}
return code;
}
本文介绍了一种使用JavaScript生成随机验证码的方法。通过定义字符集和验证码长度,该函数能够创建包含字母与数字组合的验证码。适用于网站登录或其他需要验证用户输入的场景。
421

被折叠的 条评论
为什么被折叠?



