Skip to content

Instantly share code, notes, and snippets.

@enishant
Created May 9, 2019 15:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save enishant/4ba920c71f338e83c7089dc5d6f33a64 to your computer and use it in GitHub Desktop.
Save enishant/4ba920c71f338e83c7089dc5d6f33a64 to your computer and use it in GitHub Desktop.
Create Random Password
function randomPassword(length=8,input='alpha-numeric') {
var alphabet = 'abcdefghijklmnopqrstuvwxyz';
var password = '';
if(input == 'alpha') {
alphabet = 'abcdefghijklmnopqrstuvwxyz';
} else if(input == 'alpha-caps') {
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
} else if(input == 'alpha-numeric') {
alphabet = 'abcdefghijklmnopqrstuvwxyz1234567890';
} else if(input == 'alpha-numeric-caps') {
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
} else if(input == 'alpha-numeric-symbols') {
alphabet = 'abcdefghijklmnopqrstuvwxyz1234567890~!@#$%^&*()_+-=';
} else if(input == 'alpha-numeric-caps-symbols') {
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*()_+-=';
}
var alphabet_length = alphabet.length - 1;
for (var i = 0; i < length; i++) {
var random_number = Math.floor(Math.random() * alphabet_length) + 1;
password += alphabet[random_number];
}
return password;
}
console.log('Default : ', randomPassword());
console.log('Default with password length : ', randomPassword(20));
console.log('Alpha : ', randomPassword(12,'alpha'));
console.log('Alpha Caps : ', randomPassword(12,'alpha-caps'));
console.log('Alpha Numeric : ', randomPassword(12,'alpha-numeric'));
console.log('Alpha Numeric Caps : ', randomPassword(12,'alpha-numeric-caps'));
console.log('Alpha Numeric Symbols : ', randomPassword(12,'alpha-numeric-symbols'));
console.log('Alpha Numeric Caps Symbols : ', randomPassword(12,'alpha-numeric-caps-symbols'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment