【发布时间】:2021-05-10 23:56:07
【问题描述】:
我正在尝试制作一个带有提示的密码生成器,一切似乎都在工作,但我似乎无法让我生成的密码出现在网站上。这是我目前所拥有的:
var generateBtn = document.querySelector("#generate");
var specialCharacters = [
"!",
"@",
"#",
"$",
"%",
"^",
"&",
"*",
"(",
")",
"_",
"-",
"+",
"?",
"/",
"<",
">"
];
var upperCase = [
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z"
];
var lowerCase = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z"
];
var numeric = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
generateBtn.addEventListener("click", function () {
var passLength = prompt(
"How many characters would you like your password to contain?"
);
if (passLength < 8) {
alert("Password length must be between 8 - 128 characters");
passLength = prompt(
"Please choose a number more than 8 and less than 128 characters: "
);
}
if (passLength >= 8) {
var charInput = confirm("Would you like to use special characters?");
var numInput = confirm("Would you like to use numbers?");
var lowerCaseInput = confirm("Would you like to use lowercase letters?");
var upperCaseInput = confirm("Would you like to use uppercase letters?");
}
if (!charInput && !numInput && !lowerCaseInput && !upperCaseInput) {
alert("You must select at least one character type!");
charInput = confirm("Would you like to use special characters?");
numInput = confirm("Would you like to use numbers?");
lowerCaseInput = confirm("Would you like to use lowercase letters?");
upperCaseInput = confirm("Would you like to use uppercase letters?");
}
var passOutput = {
passLength,
charOutput: charInput,
numericOutput: numInput,
lowerCaseOutput: lowerCaseInput,
upperCaseOutput: upperCaseInput
};
console.log(passOutput);
return passOutput;
});
function writePassword() {
var password = generatePassword();
console.log(password);
var randomPassword = [];
console.log(randomPassword);
if (password.charOutput) {
for (i = 0; i < specialCharacters.length; ++i) {
randomPassword.push(specialCharacters[i]);
}
}
if (password.numericOutput) {
for (i = 0; i < numeric.length; ++i) {
randomPassword.push(numeric[i]);
}
}
if (password.lowerCaseOutput) {
for (i = 0; i < lowerCase.length; ++i) {
randomPassword.push(lowerCase[i]);
}
}
if (password.upperCaseOutput) {
for (i = 0; i < upperCase.length; ++i) {
randomPassword.push(upperCase[i]);
}
}
for (var i = 0; i < password.length; ++i) {
var randomPassword = Math.floor(
Math.random() * Math.floor(randomPassword.length)
);
passwordText.push(randomPassword);
}
console.log(generatePassword);
document.getElementById("password").value = generatePassword;
}
【问题讨论】:
-
请包括
HTML。 -
generatePassword来自哪里?首先你把它当作一个函数来调用,然后你把它分配给.value,就像它是一个字符串一样。 -
看起来您正在调用函数 generatePassword() 并将密码的值设置为返回值。但我没有看到您声明和定义该函数的任何地方。
-
我该怎么做呢?
标签: javascript html css passwords