2

Simple Password Generator Example:

function randomPassword() {
    var chars = "abcdefghijklmnopqrstuvwxyz" +
    			"ABCDEFGHIJKLMNOP" + 
                "1234567890" +
                "@\#\-!$%^&*()_+|~=`{}\[\]:\";'<>?,.\/",
    	pass  = "",
        PL    = 10;
    
    for (var x = 0; x < PL; x++) {
        var i = Math.floor(Math.random() * chars.length);
        pass += chars.charAt(i);
    }
    return pass;
}

function generate() {
    myform.row_password.value = randomPassword();
}
<form name="myform" method="post" action="">
    <table width="100%" border="0">
        <tr>
            <td>Password:</td>
            <td>
                <input name="row_password" type="text" size="40">&nbsp;
                <input type="button" class="button" value="Generate" onClick="generate();" tabindex="2">
            </td>
        </tr>
    </table>
</form>

Improving Functionality Questions

1). Obtaining All Values Within Variable

Taking the base script above, how can I call chars.length and chars.charAt(i) where chars equals all the values within Chars?

var Chars = {};
    Chars.abc   = "abcdefghijklmnopqrstuvwxyz";
    Chars.ABE   = "ABCDEFGHIJKLMNOP";
    Chars.Num   = "1234567890";
    Chars.Sym   = "@\#\-!$%^&*()_+|~=`{}\[\]:\";'<>?,.\/";

2). Implementing a checkbox system for less advanced password

To generate a less advanced password, such as not including symbox via unchecking a checkbox, how can I make it so only Chars.abc, Chars.ABE, and Chars.Num values are used?

3). Equally Divide Password Length By Chars

Round down (Password length / Chars used ), ie; the example used in this question generates a 10 character password and uses all charecters, therefore there would be a minimum of 2 of each Chars.

4
  • 4
    My first thought when seeing anyone trying to write their own password functions is: don't. It's a fun exercise, but others have written much more secure, flexible packages already. Commented Dec 21, 2016 at 17:08
  • Thank you for your advice @AutumnLeonard however I've been finding a lot of password generators and strength checkers don't all have the functions I'd like. I'm wanting to somewhat merge some features others have for a simple, easy to use feature on my site. Commented Dec 21, 2016 at 17:22
  • 1
    chars.ABE = chars.abc.toUpperCase() Commented Dec 21, 2016 at 20:37
  • Thank you @KevinB, not only is this useful but you made me spot a typo whereas Chars.ABE should be Chars.ABC Commented Dec 22, 2016 at 2:21

1 Answer 1

1

The 3rd functionality is missing and will probably be way more sophisticated. But this is a simple solution to the 1st and 2nd ones.

var output = document.getElementsByTagName('output')[0];

var Chars = {};
    Chars.length = 16;
    Chars.abc   = "abcdefghijklmnopqrstuvwxyz";
    Chars.ABE   = "ABCDEFGHIJKLMNOP";
    Chars.Num   = "1234567890";
    Chars.NumRequired = true;
    Chars.Sym   = "@\#\-!$%^&*()_+|~=`{}\[\]:\";'<>?,.\/";

var generator = new randomPasswordGenerator(Chars);

var simpleGenerator = new randomPasswordGenerator({
    length: 6,
    abc: 'abc',
    Num: '0'
});

var button = document.getElementsByTagName('button')[0];
    button.addEventListener('click', clickFunction);

var checkbox = document.getElementsByTagName('input')[0];

function clickFunction () {
  if (checkbox.checked) output.textContent = simpleGenerator.randomPassword();
  else output.textContent = generator.randomPassword();
}

function randomPasswordGenerator(opts) {
    for(var p in opts) this[p] = opts[p];
    this.randomPassword = randomPassword;
}

function randomPassword() {
    var chars = (this.abc || "") +
                (this.ABE || "") + 
                (this.Num || "") +
                (this.Sym || ""),
    	pass  = [],
        PL    = this.length;
    if (this.NumRequired) {
        var r = Math.floor(Math.random() * this.Num.length);
        var i = Math.floor(Math.random() * PL);
        pass[i] = this.Num[r];
    }
    for (var x = 0; x < PL; x++) {
        if(!pass[x]) {
          var i = Math.floor(Math.random() * chars.length);
          pass[x] = chars.charAt(i);
        }
    }
    return pass.join('');
}
output {
  margin: 12px;
  display: block;
  border-bottom: 1px solid
}
<button>Generate</button>
  <input type="checkbox">Simple
  <output></output>

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

2 Comments

Whoever has downrated this answer, could they please comment to as why? Also @rafaelcastrocouto, your simple version, is there a way to make it so that there is always a number in the password?
That downvote is the kinda thing that keeps me from helping more ppl. I updated the answer so you can use the property NumRequired to make sure at least one char will be a number; I guess if you read the code carefully you will make your way to better solutions!

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.