-2
<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>

How can i make a random password generator that will generate password inside an input you can use in you projects or systems

robert
  • 3
  • 3
  • 1
    Possible duplicate of [Generate random password string with requirements in javascript](https://stackoverflow.com/questions/9719570/generate-random-password-string-with-requirements-in-javascript) – Alexander Jun 03 '17 at 15:03

3 Answers3

1

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>
oviemoses
  • 402
  • 2
  • 6
1

No need to reinvent the wheel. Check out this library: https://www.npmjs.com/package/generate-password which does exactly what you want.

Duncan Lukkenaer
  • 12,050
  • 13
  • 64
  • 97
1

Try this pure JS solution:

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>

If you want to add more possible chars simply update allPossibleChars.

Ethan
  • 4,295
  • 4
  • 25
  • 44