やりたいこと
記号大文字小文字数字を含むパスワード生成
mkpass.html
<form name="makepass">
<input type="button" value="exec" onclick="makePass()">
<input type="text" size="1" id="passcount" style="text-align:center" value="10">
<input type="checkbox" id="kigou" checked>記号あり
<input type="text" size="15" name="pass" readonly="readonly">
</form>
<script type="text/javascript">
window.onload = function () {
makePass();
}
function makePass() {
var passLen = document.makepass.passcount.value;
var pass = "";
var lower = "abcdefghijklmnopqrstuvwxyz";
var upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var num = "0123456789";
var kigou = "!#$%&=/*-+^{}()";
var kigoucount,lowercount,uppercount;
var aryIndex = [];
var aryKigouNum = [];
var aryLowerNum = [];
var aryUpperNum = [];
let checkKigou = document.makepass.kigou.checked;
if (checkKigou == true) {
kigoucount = Math.floor(Math.random() * ( passLen - 3 ) + 1);
}else {
kigoucount = 0;
}
aryIndex = [...Array(Number(passLen))].map((_, i) => i);
lowercount = Math.floor(Math.random() * ( passLen - kigoucount -2 ) + 1);
uppercount = Math.floor(Math.random() * ( passLen - kigoucount - lowercount -1 ) + 1);
for(var i=0; i < kigoucount; i++){
aryKigouNum.push(aryIndex.splice(Math.floor(Math.random() * aryIndex.length), 1));
}
for(var i=0; i < lowercount; i++){
aryLowerNum.push(aryIndex.splice(Math.floor(Math.random() * aryIndex.length), 1));
}
for(var i=0; i < uppercount; i++){
aryUpperNum.push(aryIndex.splice(Math.floor(Math.random() * aryIndex.length), 1));
}
for(var i=0;i<passLen;i++){
if (aryKigouNum.find(element => element == i)) {
pass += kigou.charAt(Math.floor(Math.random() * kigou.length));
}else if (aryLowerNum.find(element => element == i)) {
pass += lower.charAt(Math.floor(Math.random() * lower.length));
}else if (aryUpperNum.find(element => element == i)) {
pass += upper.charAt(Math.floor(Math.random() * upper.length));
}else {
pass += num.charAt(Math.floor(Math.random() * num.length));
}
}
console.log("aryKigouNum:" + aryKigouNum); //aryKigouNum:3,9
console.log("aryLowerNum:" + aryLowerNum); //aryLowerNum:2,1
console.log("aryUpperNum:" + aryUpperNum); //aryUpperNum:8,6,0
console.log("aryIndex:" + aryIndex); //aryIndex:4,5,7
// 0123456789
console.log("pass:" + pass); //pass:Usf)00N8S*
document.makepass.pass.value = pass;
}
</script>
参考にさせていただいた