0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JavaScriptでパスワード生成

Last updated at Posted at 2025-01-12

やりたいこと

記号大文字小文字数字を含むパスワード生成

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>

参考にさせていただいた

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?