I am currently working on random password generator. I want my generator to produce a changeable number of character from a certain type. I want the character types to be: capital letters, regular letters, numbers and special characters (!@#$%^&*)
For example: the output of (number:3, letters:2, capital letters:1, (!@#$%^&*):2) will be:" *a49s@1R "
but my current code is not working.
function myFunction() {
var n = document.getElementById("myText").value;
var l = document.getElementById("myletter").value;
var cl = document.getElementById("mycapitalletter").value;
var overall = cl + l + n
var ran;
password = ""
var i = 1
/*here is the javascript method *not working* */
while (i <= overall) {
ran = Math.floor(Math.random() * 3);
if (ran == 2 && cl != 0 && overall > 0) {
var letter = "";
var choice = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
firstletter += choice.charAt(Math.floor(Math.random() * choice.length));
password = password + "" + firstletter;
overall--;
cl--;
} else {
if (ran == 1 && l != 0 && overall > 0) {
var choice = "abcdefghijklmnopqrstuvwxyz";
firstletter += choice.charAt(Math.floor(Math.random() * choice.length));
password = password + "" + firstletter;
overall--;
l--;
}
if (ran == 0 && n != 0 && overall > 0) {
firstletter = Math.floor(Math.random() * 10);
password = password + "" + firstletter;
overall--;
n--;
}
}
}
var myWindow = window.open("", "MsgWindow", "width=200,height=100 />");
myWindow.document.write("<p > This pasword is: " + password + " </p> <botton> save <botton>");
if (myWindow.closed) {
document.getElementById("msg").innerHTML = "i see your not stosfaied with your password, you can change it by pushing the botton again!";
}
}