2

Good morning,

I have this code (just an example):

function random(min, max) {
    return min + parseInt(Math.random() * (max - min + 1));
}
function generatePassword(length, charset) {
    var password = "";
    while (length--) {
        password += charset[random(0, charset.length - 1)];
    }
    return password;
}
function getNewPassword() {
    $('#password').text(generatePassword(12, "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789!?#@*&.,;:+-=()[]_"));
}

And would like to add an input field in HTML, where I can set the length of the generated password.

Can you please find with me a solution? :-)

http://jsfiddle.net/RnsTq/

14
  • 1
    Since you removed your last question whilst I was about to propose an improvement I'll post it here instead: jsfiddle.net/a2r9k/3 :) Commented Aug 25, 2013 at 14:40
  • 1
    do you mean if it's possible to "type" them in the input field? Commented Aug 25, 2013 at 14:57
  • 1
    just played around a bit: jsbin.com/odosoy/97/edit something more stylish :) change the charset to what ever you want and see the generated password will contain only the characters from the charset... click generate also multiple times to get more passwords - enjoy Commented Aug 25, 2013 at 16:45
  • 1
    removing is always easy ;) jsbin.com/odosoy/125/edit Commented Aug 27, 2013 at 20:04
  • 1
    please, create a different question with the problem ... Commented Sep 1, 2013 at 14:10

6 Answers 6

3

Instead of the 12 in the call to generatePassword use parseInt($('#len').val(), 10), where len would be the id of the input element.

0
2

There you have it: http://jsbin.com/odosoy/94/edit

Javascript

$(document).ready(function() {
  $("#generate").click(function() {
    getNewPassword();
  });

  function random(min, max) {
    return min + parseInt(Math.random() * (max - min + 1), 10);
  }
  function generatePassword(charset) {
    var length = parseInt($("#passlength").val(), 10);
    var password = "";
    while (length--) {
      password += charset[random(0, charset.length - 1)];
    }
    return password;
  }
  function getNewPassword() {
    var pass = generatePassword("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789!?#@*&.,;:+-=()[]_");
    $('#password').val(pass);
  }
});

HTML

Length: <input id="passlength"/><br>
Password: <input id="password"/><br>
<button class="btn btn-info" id="generate">Generate Password</button>

Update

Just reworked and styled your initial example a bit, nothing really special. http://jsbin.com/odosoy/97/edit and one in which the password is replaced: http://jsbin.com/odosoy/125/edit

Hope it helps.

4
  • Please put the relevant code in your answer. Demos are nice, but your answer should not rely on external ressources.
    – Bergi
    Commented Aug 25, 2013 at 12:21
  • @Bergi, just added, only wanted to be quick in answering since here the answering speed does seams like a HL2 multiplayer game... Commented Aug 25, 2013 at 12:31
  • @intuitivepixel: It works! Can you improve something now? jsfiddle.net/RnsTq/2 Commented Aug 25, 2013 at 12:51
  • 1
    @MaximilianFuchs, improved: jsfiddle.net/RnsTq/3 you forgot to add base10 to parseInt in the random function Commented Aug 25, 2013 at 12:54
1

look at this:

function gen_random(len){
    var charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    var tempChar = charset.split('');
    for (var i = 0; i < len; ++i) {
        password += tempChar[Math.floor(Math.random() * (26+26+10)) + 1];           
    }
    return password;    
}
1

Try to add a new text field and take it's value before you generate your password.

var passwordLength = $('#maxLength').val(); $('#password').val(generatePassword(passwordLength, "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789!?#@*&.,;:+-=()[]_"));

You can see a quick & dirty fiddle here: http://jsfiddle.net/nininho/pG5A7/1/

0
0

Try something like this:-

Math.random().toString(36).substring(7);
0
0

The best way to generate a password in JS is to use a lib such as https://github.com/bermi/password-generator

That will generate you a password with a customizable length that even can be memorable.

There's also several example on the link provided.

Not the answer you're looking for? Browse other questions tagged or ask your own question.