今天要把许多网站的密码重新设置,所以自己写了个密码生成器。比较简陋,不过在简单的用途上应该算够用了。
您希望密码中包含哪些内容?
阿拉伯数字(0-9)
大写英文字母(A-Z)
小写英文字母(a-z)
特殊符号(键盘上除了\的所有输入符号)
请设置密码位数:
生成结果:
代码如下:
<p>您希望密码中包含哪些内容?</p>
<input type="checkbox" name="pwType" value="Number"> 阿拉伯数字(0-9)<br>
<input type="checkbox" name="pwType" value="Upper"> 大写英文字母(A-Z)<br>
<input type="checkbox" name="pwType" value="Lower"> 小写英文字母(a-z)<br>
<input type="checkbox" name="pwType" value="Punctuation"> 特殊符号(键盘上除了\的所有输入符号)<br><br>
请设置密码位数:<input type="text" name="pwLength" value="10"><br><br>
生成结果:<input type="text" name="outputPw" /><br><br>
<button name="createPwButton">生成密码</button>
<script type="text/javascript">
function createPW () {
var allPw={
"Number":"0123456789",
"Upper":"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"Lower":"abcdefghijklmnopqrstuvwxyz",
"Punctuation":"~!@#$%^&*()_+[]{}|`;:,./?><-="
}
var allowPw="";
for (var i = 0; i < document.getElementsByName("pwType").length; i++) {
if (document.getElementsByName("pwType")[i].checked) {
allowPw+=allPw[document.getElementsByName("pwType")[i].value];
};
};
var pwLength=parseInt(document.getElementsByName("pwLength")[0].value,10);
var pwResult="";
for (var i = 0; i < pwLength; i++) {
var nowRandom=parseInt(Math.random()*allowPw.length,10);
pwResult+=allowPw.substring(nowRandom, nowRandom+1);
};
document.getElementsByName("outputPw")[0].value=pwResult;
}
document.getElementsByName("createPwButton")[0].onclick=function () {
createPW();
}
</script>