javascript生成10位以上随机密码
JS代码功能
随机密码长度10位以上;
随机密码包含特殊字符;
随机密码包含英文大小写;
JavaScript语法
代码复制到本地保存XXX.html,使用浏览器打开即可运行。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
< html >
< head >
< title >随机生成密码</ title >
< meta charset = "utf8" >
< script >
function getmm(){
var amm=['!','@','#','$','%','&','*','(',')','_'];//定义特殊字符数组
var tmp=Math.floor(Math.random()*10);//生成0-9随机数
var s=tmp;
s=s+amm[tmp];//取特殊字符
//生成大写字母
for(var i=0;i< 4 ;i++){
tmp = Math .floor(Math.random()*26);
s =s+String.fromCharCode(65+tmp);
}
//生成小写字母
for(var i = 0 ;i<4;i++){
tmp = Math .floor(Math.random()*26);
s =s+String.fromCharCode(97+tmp);
}
document.getElementById('mm') .innerHTML = s ;
}
</script>
</ head >
< textarea id = "mm" >
生成10位随机密码
</ textarea >
< button onclick = "getmm()" >生成密码</ button >
</ html >
|
使用到的方法
random();//返回 0 ~ 1 之间的随机数,包含 0 不包含 1。
Math.floor(Math.random()*n);//可均衡获取 0 到 n-1 的随机整数。
fromCharCode() ;//可接受一个指定的 Unicode 值,然后返回一个字符串。
document.getElementById('mm').innerHTML=xxx//将html id为mm的值设为xxx。
οnclick=js函数//鼠标点击时调用js函数。