Advertisement
IsenfireLDC

<Javascript> Random Password Generator (Javascript and HTML)

May 5th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. NOTE: If you want to run this program, you can do two different things.
  3. 1.) You can open the console in your browser (Ctrl + Shift + I in Google Chrome)
  4.     and copy this code into it.  Hit Enter, then type 'pwdGen()' (without the single quotes)
  5.     and hit Enter again.  The program will then run.
  6.  
  7. 2.) Download a code editor or IDE (I used Notepad++ to make this.  Here is the site: https://notepad-plus-plus.org/)
  8.     Download or copy in both this code and another of my Pastebins entitled '<HTML> Random Password Generator (Javascript and HTML)'
  9.     into separate documents.  I have instructions for linking them together on my HTML.
  10.     After everything is linked, click on the 'Run' drop-down menu and click on whichever browser you want to do.
  11.     Then all you need to do is click the button, and the program will run.
  12. */
  13.  
  14.  
  15. function pwdGen () {    //Defines a new function that is run using the button in the HTML section
  16.    
  17.     var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';   //Three lists that have all the characters that my password generator can use
  18.     var numb = '1234567890';
  19.     var punc = "`~!@#$%^&*()_+-=}{[];'\":,./<>?\|";
  20.  
  21.     var possibleChars = alpha + numb;    //Combines the alpha and numb strings above
  22.  
  23.     function nextChar(chars) {    //Defines a new function that will randomly select characters from the strings
  24.         var loc = Math.round(Math.random()*chars.length - 1);
  25.  
  26.         /*Creates a variable loc and gives it the value of a random number
  27.          (look Math.random() and Math.round() up on your own) which is multiplied
  28.          by the length of the string (created by combining the original strings)*/
  29.  
  30.         return chars.substring(loc, loc + 1);   //uses the number in loc to take one character out of the combined strings
  31.     };
  32.  
  33.     /*Makes a pop-up to ask if the user wants to use the program*/
  34.     permissionRandGen = confirm('Would you like a random password suggestion?');
  35.  
  36.    
  37.     if (permissionRandGen == true) {    //If the user wants to use the program, this starts to generate the function
  38.         var lenPwd = prompt('How long do you want your password?');   //Asks for length of the password
  39.         var usePunc = confirm('Do you want punctuation in your password?');    //Asks the user if they want characters
  40.  
  41.         /*If the user want punctuation, adds it to the possible characters*/
  42.         if (usePunc == true) {
  43.             possibleChars = possibleChars + punc;
  44.         };
  45.  
  46.         /*If the length is between 1 and 25, the program will run*/
  47.         if (lenPwd > 0 && lenPwd < 26) {
  48.             var newPwd = "";    //Creates a variable the will store the password
  49.  
  50.             /*Creates new characters until the password it complete*/
  51.             for (var i = 0; i < lenPwd; i++) {
  52.                 newPwd += nextChar(possibleChars);
  53.             };
  54.  
  55.         /*Creates a new element in the HTML section, adds text to it, and then adds the element to <body>*/
  56.         /*Shows the user their new password suggestion*/
  57.         var docElementNewPwd = document.createElement("p")
  58.         var docTextNewPwd = document.createTextNode("Your new password is: " + newPwd);
  59.         docElementNewPwd.appendChild(docTextNewPwd);
  60.         document.body.appendChild(docElementNewPwd);
  61.         /*NOTE: If the program is run multiple times without refreshing the page, you will be able to see all of the passwords generated*/
  62.  
  63.         /*If the password length is longer or shorter than the limit, this informs the user*/
  64.         } else {
  65.             var dELenError = document.createElement("p");
  66.             var dTLenError = document.createTextNode("I can only generate passwords between 1 and 25 characters long.");
  67.             dELenError.appendChild(dTLenError);
  68.             document.body.appendChild(dELenError);
  69.         };
  70.        
  71.     /*If the user chooses not to run the program, this creates a heading (<h2>) element to ask a rhetorical question*/
  72.     } else {
  73.         var dEUserEnd = document.createElement("h2");
  74.         var dTUserEnd = document.createTextNode("Well then, why did you run me?");
  75.         dEUserEnd.appendChild(dTUserEnd);
  76.         document.body.appendChild(dEUserEnd);
  77.     };
  78. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement