如何使用窗口提示/确认以及使用 math.floor/math.random 的函数通过 HTML 按钮生成密码? (Javascript/HTML)

发布于 2025-01-10 14:31:02 字数 4218 浏览 0 评论 0原文

我正在尝试使用 javascript 创建一个密码生成器,并创建一个与带有“点击”事件侦听器的 HTML 按钮绑定的生成密码函数。

我已经完成了获取用户输入的所有工作(创建了密码长度的窗口提示,以及大写、小写、特殊字符和数字的窗口确认)。我创建了几个函数,它们从每种类型的给定字符代码范围生成随机输出。

如何将这些随机发生器函数附加到我的generatePassword函数中,以便它们返回与用户定义的规范和长度相关的随机值?

我尝试过创建这些函数的数组,但没有成功。我不确定如何以给定长度迭代数组,并且我尝试过的所有操作都返回这些函数值未定义。我只使用了 javascript 大约 2 周。

//Window Prompts for user/ Event Listeners

function userinputLength(){
  var passLength = Number(window.prompt("How many characters in length should your password be? Enter a number between 8 and 128", ""));
   //Comparing input from user to see if it's valid 
    if (passLength >= 129 || passLength <= 7 ) {
      window.alert("That's outside of the given range, try again")
      userinputLength()
    }
    
    else {
   
    if (typeof(passLength) === "number"){
      console.log(passLength);
      window.alert("You have chosen " + passLength);
    } else if (typeof(passLength) === "string", "char"){
      console.log("NOT A NUMBER");
      window.alert("That's not a number, try again.");
      userinputLength()
    }
  }
    
  
  
  //Create a variable containing prompt/alert/something window for user defined types of characters (Upper-case, Lower-Case, Special Characters(MAY NEED MORE THAN ONE for letters, case, numbers, special characters)
};
//function asking user for uppercase or not
function passCase1() { 
  var passCaseconfirm1 = window.confirm("Do you want Upper case characters?")
      if (passCaseconfirm1 === true) {
        window.alert("You have chosen to use upper case characters.")
      }else {
        window.alert("You have chosen NOT to use upper case characters.")
      }
    console.log(passCaseconfirm1);

};
// function asking user if they want lower case characters or not
function passCase2() { 
  var passCaseconfirm2 = window.confirm("Do you want lower case characters?")
      if (passCaseconfirm2 === true) {
        window.alert("You have chosen to use lower case characters.")
      }else {
        window.alert("You have chosen NOT to use lower case characters.")
      }
      //USE UPPERCASE ARRAY
    
    console.log(passCaseconfirm2);

};

//function asking user for special characters or not
function passChar(){
  var passCharconfirm = window.confirm("Do you want to use special characters or not?");
  if (passCharconfirm) {
    window.alert("You have chosen to use special characters");
    //USE SPECIAL CHARACTERS ARRAY
  } else {
    window.alert("You have chosen NOT to use special characters");
    //DONT USE SPECIAL CHARACTERS ARRAY
  }
  console.log(passCharconfirm);
}  
// Function asking user if they want random numbers or not
function passNum(){
  var passNumconfirm = window.confirm("Do you want to use numbers or not?");
  if (passNumconfirm) {
    window.alert("You have chosen to use number");
    //USE SPECIAL CHARACTERS ARRAY
  } else {
    window.alert("You have chosen NOT to use numbers");
    //DONT USE SPECIAL CHARACTERS ARRAY
  }
  console.log(passNumconfirm);
}  

//Functions to get random output from character code. 


function getRandomLower() {
  return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
}

function getRandomUpper() {
  return String.fromCharCode(Math.floor(Math.random() * 26) + 65);
}

function getRandomNumber() {
  return String.fromCharCode(Math.floor(Math.random() * 10) + 48);
}

function getRandomSymbol() {
  const symbols = "!@#$%^&*()_+|\={}[]>?<;'./,"
  return symbols[Math.floor(Math.random() *  symbols.length)];
}

//Generate Password function and associated function calls. 
function generatePassword() {
  userinputLength();
  passCase1();
  passCase2();
  passChar(); 
  passNum();
  //NEED MORE FUNCTION CALLS HERE THAT DETERMINE IF EACH CASE IS TRUE OR FALSE AND RETURNS VALUE BASED ON LENGTH
  };

// Get references to the #generate element
var generateBtn = document.querySelector("#generate");

// Write password to the #password input
function writePassword() {
  var password = generatePassword();
  var passwordText = document.querySelector("#password");
  passwordText.value = password;
}

// Add event listener to generate button
generateBtn.addEventListener("click", writePassword);

//CALL FUNCTIONS

I'm trying to create a password generator using javascript and a generate password function tied to an HTML button with a 'click' event listener.

I have done all the work to get user-input, (created window prompt for password length, and window confirms for upper case, lower case, special characters, and numbers). I have created several functions which generate random outputs from a given range of the character code for each type.

How would I attach these randomizer functions into my generatePassword function so that they give back random values that correlate to the user-defined specifications and length?

I have tried creating an array of these functions, with no luck. I'm not sure how to iterate through the array at the given length, and everything I have tried has returned that these functions values are not defined. I have only been using javascript for about 2 weeks.

//Window Prompts for user/ Event Listeners

function userinputLength(){
  var passLength = Number(window.prompt("How many characters in length should your password be? Enter a number between 8 and 128", ""));
   //Comparing input from user to see if it's valid 
    if (passLength >= 129 || passLength <= 7 ) {
      window.alert("That's outside of the given range, try again")
      userinputLength()
    }
    
    else {
   
    if (typeof(passLength) === "number"){
      console.log(passLength);
      window.alert("You have chosen " + passLength);
    } else if (typeof(passLength) === "string", "char"){
      console.log("NOT A NUMBER");
      window.alert("That's not a number, try again.");
      userinputLength()
    }
  }
    
  
  
  //Create a variable containing prompt/alert/something window for user defined types of characters (Upper-case, Lower-Case, Special Characters(MAY NEED MORE THAN ONE for letters, case, numbers, special characters)
};
//function asking user for uppercase or not
function passCase1() { 
  var passCaseconfirm1 = window.confirm("Do you want Upper case characters?")
      if (passCaseconfirm1 === true) {
        window.alert("You have chosen to use upper case characters.")
      }else {
        window.alert("You have chosen NOT to use upper case characters.")
      }
    console.log(passCaseconfirm1);

};
// function asking user if they want lower case characters or not
function passCase2() { 
  var passCaseconfirm2 = window.confirm("Do you want lower case characters?")
      if (passCaseconfirm2 === true) {
        window.alert("You have chosen to use lower case characters.")
      }else {
        window.alert("You have chosen NOT to use lower case characters.")
      }
      //USE UPPERCASE ARRAY
    
    console.log(passCaseconfirm2);

};

//function asking user for special characters or not
function passChar(){
  var passCharconfirm = window.confirm("Do you want to use special characters or not?");
  if (passCharconfirm) {
    window.alert("You have chosen to use special characters");
    //USE SPECIAL CHARACTERS ARRAY
  } else {
    window.alert("You have chosen NOT to use special characters");
    //DONT USE SPECIAL CHARACTERS ARRAY
  }
  console.log(passCharconfirm);
}  
// Function asking user if they want random numbers or not
function passNum(){
  var passNumconfirm = window.confirm("Do you want to use numbers or not?");
  if (passNumconfirm) {
    window.alert("You have chosen to use number");
    //USE SPECIAL CHARACTERS ARRAY
  } else {
    window.alert("You have chosen NOT to use numbers");
    //DONT USE SPECIAL CHARACTERS ARRAY
  }
  console.log(passNumconfirm);
}  

//Functions to get random output from character code. 


function getRandomLower() {
  return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
}

function getRandomUpper() {
  return String.fromCharCode(Math.floor(Math.random() * 26) + 65);
}

function getRandomNumber() {
  return String.fromCharCode(Math.floor(Math.random() * 10) + 48);
}

function getRandomSymbol() {
  const symbols = "!@#$%^&*()_+|\={}[]>?<;'./,"
  return symbols[Math.floor(Math.random() *  symbols.length)];
}

//Generate Password function and associated function calls. 
function generatePassword() {
  userinputLength();
  passCase1();
  passCase2();
  passChar(); 
  passNum();
  //NEED MORE FUNCTION CALLS HERE THAT DETERMINE IF EACH CASE IS TRUE OR FALSE AND RETURNS VALUE BASED ON LENGTH
  };

// Get references to the #generate element
var generateBtn = document.querySelector("#generate");

// Write password to the #password input
function writePassword() {
  var password = generatePassword();
  var passwordText = document.querySelector("#password");
  passwordText.value = password;
}

// Add event listener to generate button
generateBtn.addEventListener("click", writePassword);

//CALL FUNCTIONS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文