-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

1

3 Answers 3

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>

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.