Created
May 29, 2015 20:14
-
-
Save eric-miller2129/9a1eedaf305ac1b0a256 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var rsg = rsg || {}; | |
// (function () { | |
// 'use strict'; | |
// }()); | |
rsg = (function(){ | |
'use strict'; | |
var string_generator = {}; | |
string_generator.generateString = function () { | |
var availableCharacters = '', | |
newString = '', | |
stringLower = 'abcdefghijklmnopqrstuvwxyz0123456789', | |
stringUpper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', | |
stringSpecial = '!@$%^&?', | |
stringDefault = 12, | |
characterArray = [], | |
chkLower = document.getElementById('chk-alpha-lower'), | |
chkUpper = document.getElementById('chk-alpha-upper'), | |
chkSpecial = document.getElementById('chk-special-characters'), | |
stringLength = document.getElementById('txt-string-length').value, | |
stringResult = document.getElementById('txt-result'); | |
// If nothing is checked, prompt the user to check something | |
if (!chkLower.checked && !chkUpper.checked && !chkSpecial.checked) { | |
alert('Please select at least one option'); | |
return false; | |
} | |
// If lowercase characters are allowed, push them to the characterArray | |
if (chkLower.checked) { | |
characterArray.push(stringLower); | |
} | |
// If uppercase characters are allowed, push them to the characterArray | |
if (chkUpper.checked) { | |
characterArray.push(stringUpper); | |
} | |
// If special characters are allowed, push them to the characterArray | |
if (chkSpecial.checked) { | |
characterArray.push(stringSpecial); | |
} | |
// If no string length is entered, set a default | |
if (stringLength === '') { | |
stringLength = stringDefault; | |
} else { | |
parseInt(stringLength); | |
} | |
// Convert characterArray to a string and set availableCharacters | |
availableCharacters = characterArray.toString(); | |
// While the length of the new string is less than the desired length, add a random character from the availableCharacters | |
do { | |
var wildcard = Math.random() * (stringLength - 1); | |
newString += availableCharacters.substring((wildcard - 1), wildcard); | |
} while (newString.length < (stringLength)); | |
stringResult.value = newString; | |
console.log(availableCharacters); | |
}; | |
string_generator.init = function () { | |
var submitButton = document.getElementById('btn-submit'); | |
submitButton.onclick = function (e) { | |
e.preventDefault(); | |
string_generator.generateString(); | |
}; | |
}; | |
return string_generator; | |
}()); | |
(function(){ | |
rsg.init(); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment