随机密码生成源码
html
<!DOCTYPE html>
<head>
<title>随机密码生成</title>
<meta charset="UTF-8" />
<script lang="javascript">
function getNumric() {
return Math.floor(Math.random() * 10)
}
function getUpperLetter() {
var letterIndexBaseA = Math.floor(Math.random() * (90 - 65 + 1))
return String.fromCharCode(65 + letterIndexBaseA);
}
function getLowerLetter() {
var letterIndexBasea = Math.floor(Math.random() * (122 - 97 + 1))
return String.fromCharCode(97 + letterIndexBasea);
}
function getAllSpecialChar() {
switch (Math.floor(Math.random() * 4)) {
case 0:
return String.fromCharCode(33 + Math.floor(Math.random() * (47 - 33 + 1)));
case 1:
return String.fromCharCode(58 + Math.floor(Math.random() * (64 - 58 + 1)));
case 2:
return String.fromCharCode(91 + Math.floor(Math.random() * (96 - 91 + 1)));
case 3:
return String.fromCharCode(123 + Math.floor(Math.random() * (126 - 123 + 1)));
}
}
function getInputedSpecialChar() {
var inputedChars = document.getElementsByName("specialChars")[0].value.trim();
return inputedChars.charAt(Math.floor(Math.random() * inputedChars.length));
}
function generateRandomPassword() {
var randomString = '';
var functionArr = [];
document.getElementsByName("upperCaseTrigger").forEach(ele => {
if (!ele.checked) {
return;
}
if (ele.value == 0) {
return;
}
functionArr.push(getUpperLetter);
})
document.getElementsByName("lowerCaseTrigger").forEach(ele => {
if (!ele.checked) {
return;
}
if (ele.value == 0) {
return;
}
functionArr.push(getLowerLetter);
})
document.getElementsByName("numericTrigger").forEach(ele => {
if (!ele.checked) {
return;
}
if (ele.value == 0) {
return;
}
functionArr.push(getNumric);
})
document.getElementsByName("specialCharTrigger").forEach(ele => {
if (!ele.checked) {
return;
}
if (ele.value == 0) {
return;
}
if (document.getElementsByName("specialChars")[0].value.trim().length > 0) {
functionArr.push(getInputedSpecialChar);
} else {
functionArr.push(getAllSpecialChar);
}
})
if (functionArr.length == 0) {
document.getElementById("passwordArea").innerText = "全禁用了拿你生成密码啊?";
return;
}
for (var i = 0; i < document.getElementsByName("pwdLength")[0].value; i++) {
randomString = randomString + functionArr[Math.floor(Math.random() * functionArr.length)]();
}
document.getElementById("passwordArea").innerText = randomString;
}
</script>
</head>
<body>
<span id="passwordArea"></span>
<br /><br /><br />
<!-- 密码长度配置 -->
<label for="pwdLength">密码长度</label>
<input id="pwdLength" name="pwdLength" type="number" value="16" />
<br />
<!-- 是否启用大写字母 -->
<input checked type="radio" id="enableUpperCase" name="upperCaseTrigger" value="1" />
<label for="enableUpperCase">启动</label>
<input type="radio" id="disableUpperCase" name="upperCaseTrigger" value="0" />
<label for="disableUpperCase">禁用</label>
大写字母
<br />
<!-- 是否启用小写字母 -->
<input type="radio" checked id="enableLowerCase" name="lowerCaseTrigger" value="1" />
<label for="enableLowerCase">启动</label>
<input type="radio" id="disableLowerCase" name="lowerCaseTrigger" value="0" />
<label for="disableLowerCase">禁用</label>
小写字母
<br />
<!-- 是否启用数字 -->
<input type="radio" checked id="enableNumeric" name="numericTrigger" value="1" />
<label for="enableNumeric">启动</label>
<input type="radio" id="disableNumeric" name="numericTrigger" value="0" />
<label for="disableNumeric">禁用</label>
数字
<br />
<!-- 是否启用特殊字符 -->
<input type="radio" checked id="enableSpecialChar" name="specialCharTrigger" value="1" />
<label for="enableSpecialChar">启动</label>
<input type="radio" id="disableSpecialChar" name="specialCharTrigger" value="0" />
<label for="disableSpecialChar">禁用</label>
<label for="specialChar">特殊字符(启用时,置空则使用所有特殊字符)</label>
<input id="specialChars" name="specialChars" type="text" />
<br />
<button onclick="generateRandomPassword()">生成密码</button>
</body>
</html>