【问题标题】:Generating random password into input with input length using Javascript使用Javascript生成具有输入长度的随机密码输入
【发布时间】:2017-11-04 19:42:00
【问题描述】:
<form name="pgenerate">
<input type="text" size=18 name="output">
<input type="button" value="Generate Password" ><br />
<b>Password Length:</b> <input type="text" name="thelength" size=3 value="7">
</form>

我如何制作一个随机密码生成器,它将在您可以在项目或系统中使用的输入中生成密码

【问题讨论】:

标签: javascript


【解决方案1】:

试试这个纯 JS 解决方案:

function generateRandomPassword (passwordLength) {
    var outputPassword = "";
    var allPossibleChars  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    for (var i = 0; i < passwordLength; i++) {
        outputPassword += allPossibleChars.charAt(Math.floor(Math.random() * allPossibleChars.length));
    }
    return outputPassword;
}
<form name="pgenerate">
<input type="text" size=18 id="output" name="output">
<input type="button" value="Generate Password" onclick="document.getElementById('output').value = generateRandomPassword(document.getElementById('thelength').value);"><br />
<b>Password Length:</b> <input type="text" id="thelength" name="thelength" size=3 value="7">
</form>

如果您想添加更多可能的字符,只需更新allPossibleChars

【讨论】:

    【解决方案2】:

    无需重新发明轮子。查看这个库:https://www.npmjs.com/package/generate-password,它完全符合您的要求。

    【讨论】:

      【解决方案3】:

      var keylist="abcdefghijklmnopqrstuvwxyz123456789"
      var temp=''
       
      function generatepass(plength){
      temp=''
      for (i=0;i<plength;i++)
      temp+=keylist.charAt(Math.floor(Math.random()*keylist.length))
      return temp
      }
       
      function populateform(enterlength){
      document.pgenerate.output.value=generatepass(enterlength)
      }
      <form name="pgenerate">
      <input type="text" size=18 name="output">
      <input type="button" value="Generate Password" onClick="populateform(this.form.thelength.value)"><br />
      <b>Password Length:</b> <input type="text" name="thelength" size=3 value="7">
      </form>

      【讨论】:

        猜你喜欢
        • 2020-04-17
        • 1970-01-01
        • 2020-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-27
        • 2017-03-22
        • 1970-01-01
        相关资源
        最近更新 更多