我的作业代码应该提示用户输入密码长度和字符类型。出现提示时,我希望用户最多可以输入 3 个答案(即“大写、小写、特殊”),但是当我运行它时,它只接受一个答案。这是我的代码,请帮助:
var length = Number(prompt("Enter a password length between 8 and 128")),
charType = prompt("Enter up to 3 character types: special, numeric, uppercase, lowercase."),
password = generatePassword();
document.getElementById("display").value = password;
document.getElementById('copy-btn').addEventListener('click', copyPassword);
function generatePassword() {
var charSets = {
lowercase: 'abcdefghijklmnopqrstuvwxyz',
uppercase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
numeric: '0123456789',
special: ' !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
};
var charSet = charSets[charType.toLowerCase()] || charSets.lowercase;
var retVal = "";
for (var i = 0; i < length; i++) {
retVal += charSet.charAt(Math.floor(Math.random() * charSet.length));
}
return retVal;
}
function copyPassword() {
document.getElementById("display").select();
document.execCommand("Copy");
alert("Password copied to clipboard!");
}
慕妹3242003
相关分类