1function generatePassword(passwordLength) {
2 var numberChars = "0123456789";
3 var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
4 var lowerChars = "abcdefghijklmnopqrstuvwxyz";
5 var allChars = numberChars + upperChars + lowerChars;
6 var randPasswordArray = Array(passwordLength);
7 randPasswordArray[0] = numberChars;
8 randPasswordArray[1] = upperChars;
9 randPasswordArray[2] = lowerChars;
10 randPasswordArray = randPasswordArray.fill(allChars, 3);
11 return shuffleArray(randPasswordArray.map(function(x) { return x[Math.floor(Math.random() * x.length)] })).join('');
12}
13
14function shuffleArray(array) {
15 for (var i = array.length - 1; i > 0; i--) {
16 var j = Math.floor(Math.random() * (i + 1));
17 var temp = array[i];
18 array[i] = array[j];
19 array[j] = temp;
20 }
21 return array;
22}
23
24alert(generatePassword(12));
1 const randomStringMake = (count)=> {
2 const letter = "0123456789ABCDEFGHIJabcdefghijklmnopqrstuvwxyzKLMNOPQRSTUVWXYZ0123456789abcdefghiABCDEFGHIJKLMNOPQRST0123456789jklmnopqrstuvwxyz";
3 let randomString = "";
4 for (let i = 0; i < count; i++) {
5 const randomStringNumber = Math.floor(1 + Math.random() * (letter.length - 1));
6 randomString += letter.substring(randomStringNumber, randomStringNumber + 1);
7 }
8 return randomString
9}
10
11console.log(randomStringMake(10))
1var Password = {
2
3 _pattern : /[a-zA-Z0-9_\-\+\.]/,
4
5
6 _getRandomByte : function()
7 {
8 // http://caniuse.com/#feat=getrandomvalues
9 if(window.crypto && window.crypto.getRandomValues)
10 {
11 var result = new Uint8Array(1);
12 window.crypto.getRandomValues(result);
13 return result[0];
14 }
15 else if(window.msCrypto && window.msCrypto.getRandomValues)
16 {
17 var result = new Uint8Array(1);
18 window.msCrypto.getRandomValues(result);
19 return result[0];
20 }
21 else
22 {
23 return Math.floor(Math.random() * 256);
24 }
25 },
26
27 generate : function(length)
28 {
29 return Array.apply(null, {'length': length})
30 .map(function()
31 {
32 var result;
33 while(true)
34 {
35 result = String.fromCharCode(this._getRandomByte());
36 if(this._pattern.test(result))
37 {
38 return result;
39 }
40 }
41 }, this)
42 .join('');
43 }
44
45};
1const alpha = 'abcdefghijklmnopqrstuvwxyz';
2const calpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
3const num = '1234567890';
4const specials = ',.!@#$%^&*';
5const options = [alpha, alpha, alpha, calpha, calpha, num, num, specials];
6let opt, choose;
7let pass = "";
8for ( let i = 0; i < 8; i++ ) {
9 opt = Math.floor(Math.random() * options.length);
10 choose = Math.floor(Math.random() * (options[opt].length));
11 pass = pass + options[opt][choose];
12 options.splice(opt, 1);
13}
14console.log(pass);
15
1<input type='text' id='p'/><br/>
2<input type='button' value ='generate' onclick='document.getElementById("p").value = Password.generate(16)'>