如何用js实现随机生成六位数字密码
发布网友
发布时间:2022-05-08 14:58
我来回答
共4个回答
热心网友
时间:2022-04-20 08:59
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script type="text/javascript">
//16进制(数值+字母)
function random1(len) {
var pwd = "";
for(var idx = 0; idx < len; idx ++){
pwd = pwd + ((Math.random() * 16) & (0x5 | 0x9)).toString(16);
}
return pwd;
}
//ASCII (数值+字母+特殊字符)
function random2(len){
//ASCII 33 ~ 126
var pwd = "";
for(var idx = 0; idx < len; idx ++){
pwd = pwd + String.fromCharCode(parseInt(Math.random() * 93 + 33));
}
return pwd;
}
//纯数字
function random3(len){
var pwd = "";
for(var idx = 0; idx < len; idx ++){
var seed = parseInt(Math.random() * 9);
pwd = pwd + seed;
}
return pwd;
}
function create(){
var result = document.getElementById("result1");
for(var i = 0; i < 10; i++){
result.innerText = result.innerText + random1(6) + "\n";
}
var result = document.getElementById("result2");
for(var i = 0; i < 10; i++){
result.innerText = result.innerText + random2(6) + "\n";
}
var result = document.getElementById("result3");
for(var i = 0; i < 10; i++){
result.innerText = result.innerText + random3(6) + "\n";
}
}
</script>
</head>
<body>
<div id="result1" style="border:1px solid #e0e0e0;width: 200px; height: 300px;margin-top: 10px;float: left;">
</div>
<div id="result2" style="border:1px solid #e0e0e0;width: 200px; height: 300px;margin-top: 10px;margin-left: 20px;float: left;">
</div>
<div id="result3" style="border:1px solid #e0e0e0;width: 200px; height: 300px;margin-top: 10px;margin-left: 20px;float: left;">
</div>
<button onclick="create()" style="padding: 3px 5px;margin: 5px 20px;">Random</button>
</body>
</html>
热心网友
时间:2022-04-20 10:17
写一个数组里面存上数字和字母,然后利用random函数随机6个字符串拼接,当然一般验证码都应该是后台生成的
热心网友
时间:2022-04-20 11:51
random() 方法可返回介于 0 ~ 1 之间的一个随机数。
document.write(Math.random())
输出 0.3819797304347996
乘以 1000000 再取整
var num = math.ceil( Math.random()*100000);
热心网友
时间:2022-04-20 13:43
String(Math.random()).substring(2, 8)