やりたいこと
記号大文字小文字数字を含むパスワード生成
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() {
const passLen = document.makepass.passcount.value;
let pass = "";
const lower = "abcdefghijklmnopqrstuvwxyz";
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const num = "0123456789";
const kigou = "!#$%&=/*-+^{}()";
let kigoucount,lowercount,uppercount;
let aryIndex = [];
let aryKigouNum = [];
let aryLowerNum = [];
let aryUpperNum = [];
const checkKigou = document.makepass.kigou.checked;
if (checkKigou == true) {
kigoucount = Math.floor(Math.random() * ( passLen - 3 ) + 1); //kigoucountにパスワード長から他文字最低1引いた数ランダムな数値(1以上)を格納する
}else {
kigoucount = 0;
}
aryIndex = [...Array(Number(passLen))].map((_, i) => i); //aryIndexにパスワード長のインデックス(0~)を格納する
lowercount = Math.floor(Math.random() * ( passLen - kigoucount -2 ) + 1);
uppercount = Math.floor(Math.random() * ( passLen - kigoucount - lowercount -1 ) + 1);
for(let i=0; i < kigoucount; i++){
aryKigouNum.push(aryIndex.splice(Math.floor(Math.random() * aryIndex.length), 1)); //aryIndexからkigoucount個のランダム番目のインデックス数値を取り出す
}
for(let i=0; i < lowercount; i++){
aryLowerNum.push(aryIndex.splice(Math.floor(Math.random() * aryIndex.length), 1));
}
for(let i=0; i < uppercount; i++){
aryUpperNum.push(aryIndex.splice(Math.floor(Math.random() * aryIndex.length), 1));
}
for(let i=0;i<passLen;i++){
if (aryKigouNum.find(element => element == i)) { //passのaryKigouNum番目の位置に記号を入れる
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>
参考にさせていただいた