Skip to content

Instantly share code, notes, and snippets.

@manderly
Created June 7, 2014 17:56
Show Gist options
  • Save manderly/6bb35d8889c092736cc6 to your computer and use it in GitHub Desktop.
Save manderly/6bb35d8889c092736cc6 to your computer and use it in GitHub Desktop.
JavaScript random password generator
<script>
/*Random password generator
Set length to desired password length
Tell it if you want to include letters, capital letters, numbers, and symbols
Tell it if the password MUST require a capital letter, number, or symbol
Drag into browser to generate a password
*/
var length = 10;
var lettersAllowed = true;
var capitalLettersAllowed = true;
var numbersAllowed = true;
var symbolsAllowed = true;
var mustIncludeCapitalLetter = true;
var mustIncludeNumber = true;
var mustIncludeSymbol = true;
var letters = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
var numbers = "1,2,3,4,5,6,7,8,9,0";
var symbols = "!,@,#,$,%,&";
var validChars = "";
var password = "";
var charsChosen = 0;
buildValidChoicesString();
createRandomPasswordString();
function buildValidChoicesString() {
//Build list of options
if (lettersAllowed == true) {
validChars += "," + letters;
}
if (capitalLettersAllowed == true) {
validChars += "," + letters.toUpperCase();
}
if (numbersAllowed == true) {
//added twicec to make numbers more common in the array
validChars +="," + numbers;
validChars +="," + numbers;
}
if (symbolsAllowed == true) {
//added three times to make symbols more common in the array
validChars +="," + symbols;
validChars +="," + symbols;
validChars +="," + symbols;
}
console.log("valid chars: " + validChars);
}
function insertRequiredCharacters() {
var requiredChars = "";
if (mustIncludeCapitalLetter == true) {
requiredChars += chooseLetter().toUpperCase();
charsChosen++;
}
if (mustIncludeNumber == true) {
requiredChars += chooseNumber();
charsChosen++;
}
if (mustIncludeSymbol == true) {
requiredChars += chooseSymbol();
charsChosen++;
}
return requiredChars;
}
function createRandomPasswordString() {
//Put in any "must have" characters, like a number or a capital letter
password = insertRequiredCharacters();
//Create the rest of the password string
validCharsArray = validChars.split(",");
for (charsChosen; charsChosen < length; charsChosen++) {
var randomChar = validCharsArray[Math.floor(Math.random() * validCharsArray.length)];
password += randomChar;
}
var passwordArray = password.split("");
password = shufflePassword(passwordArray);
}
function shufflePassword(array) {
//shuffle code from http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
var shuffled = array.toString().replace(/,/g ,"");
return shuffled;
}
document.write("Random password with " + length + " characters: " + password);
function chooseLetter() {
var lettersArray = letters.split(",");
var index = Math.floor(Math.random() * lettersArray.length);
return lettersArray[index];
}
function chooseSymbol() {
var symbolsArray = symbols.split(",");
var index = Math.floor(Math.random() * symbolsArray.length);
return symbolsArray[index];
}
function chooseNumber() {
var index = Math.floor(Math.random() * 10);
return index;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment