[Solved] How to generate a random password using these requirements? [closed]


Try

var specialCharacters = "~!@#$%^&*()_+=-|\\}]{[\"':;?/>.<,";
var numbers = "0123456789";
var smallLetters = "abcdefghijklmnopqrstuvwxyz";
var capitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var srcs = [specialCharacters, numbers, smallLetters, capitalLetters];

function getRandomChar(string){
    var pos = Math.floor(Math.random() * string.length);
    return string.charAt(pos);
}

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]
function shuffle(o){ //v1.0
    for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
}
function generate(){
    var len = 8 + Math.floor(Math.random() * 5);

    var array = [];
    array.push(getRandomChar(specialCharacters));
    array.push(getRandomChar(numbers));
    array.push(getRandomChar(smallLetters));
    array.push(getRandomChar(capitalLetters));

    for(var i = 4; i < len; i++){
        var pos = Math.floor(Math.random() * srcs.length);
        array.push(getRandomChar(srcs[pos]));
    }
    return shuffle(array).join('')
}

for(var i = 0; i < 10; i++){
    console.log(generate())
}

Demo: Fiddle

6

solved How to generate a random password using these requirements? [closed]