Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

random password generator?

Avatar

Level 2

Hi,

I would like a "gererate password" button that fills a filed with an alphanumeric passwrod of 8 characters or so.  Wildcard characters would be good as well.  Can anyone help me with a script that can create random passwords?  Also - I understand it may not be truly random as a response duplicated could be created every 1,233,432 times or whatever and thats OK  It would be random enought.  Thanks much.

1 Accepted Solution

Avatar

Correct answer by
Level 10

//strPassword.substr(), created some problems... somehow it could return more than one character at a time.... I don't know why but that was the problem for duplicates... so I changed it for charAt()

//Also sometimes it was possible you could only have 6 or 7 length instead of 8 all the time... I had to add (i--;) if boChar was true

//Now it should work perfectly fine!

var strPassword = "";

//Set up the length of the password

var intLength = 8;

for (var i = 0; i < intLength; i++){

          var random = Math.floor(Math.random() * 150);

          if ((random >= 35 && random <= 38) || (random == 33)|| (random == 42) || (random == 95) || (random >= 48 && random <= 57) || (random >= 63 && random <= 78)|| (random >= 80 && random <= 91) || (random == 93) || (random >=97 && random <= 110) || (random >=112 && random <= 122)){ //Insert your password in the string var

              //Store character in value 

        var strChar = String.fromCharCode(random);

        //Character is initially not contained in the string

        var boChar = false;

        for (var z = 0; z < strPassword.length; z++){

                  if (strPassword.charAt(z) == strChar){

                     //The character is contained in the string, value is now true

               boChar = true;

           }

        } 

        if (!boChar){

                  //If character is not contained in the string, the character is added to the string, otherwise it is not added

           strPassword += strChar;

        } else {

                              i--;

        }

          } else{

              i--;

    }

}

View solution in original post

9 Replies

Avatar

Level 10

Just like any other language, you can create a random string based on the ASCII Table! Using random numbers to generate random characters...

var strPassword = "";

//Set up the length of the password

var intLength = 15;

for (var i = 0; i < intLength; i++){

  //Math.Random(); returns a random number between 0 and 1

          var random = Math.floor(Math.random() * 150);

   //make sure random is a good character for your password

          if ((random >= 48 && random <= 57) || (random >= 65 && random <= 90) || (random >=97 && random <= 122)){ //Insert your password in the string var

                    strPassword += String.fromCharCode(random);

          } else{

                    i--;//if the number is not generated between the specified character above, reduce "i" value by one to have a password as long as the length specified

          }

}

xfa.host.messageBox(strPassword);

Avatar

Level 2

Wow that was great - took me a little while to figure out the ascii table worked.  I added some wild cards.  This    var random = Math.floor(Math.random() * 150);  I don't really get to be honest, but it works.  Any explination would be enlighting.

Ok - attempt to complicate this one step further.  Is there a way to prevent charcters from repeating?

Magus - thanks so much for your help.

Avatar

Level 10

Math.floor(); method returns a decimal value to the lowest value,

so (Math.floor(6.34) = 6) & (Math.floor(6.78) = 6)

Math.random(); method returns a decimal value between 0 & 1

the decimal value can be 0.642512573 or 0.2346572 or 0.85673 (randomly generated)

Multiplying this number by 150 return a minimum of Math.floor(0.0000001 * 150 ) =  0 -> to  Math.floor(0.999999 * 150) = 149

by increasing the number 150 you can use more characters in the ASCII table and can have more chances to have random characters, I used it only as an exemple

if you want to make sure different characters can be used, you can use a contain method to look if the character exists in the password

for (var i = 0; i < intLength; i++){

          var random = Math.floor(Math.random() * 150);

          if ((random >= 48 && random <= 57) || (random >= 65 && random <= 90) || (random >=97 && random <= 122)){

               //Store character in value    

               var strChar = String.fromCharCode(random);

               //Character is initially not contained in the string

               var boChar = false;

               for (var z = 0; z < strValue.length; z++){

                   if (strValue.substr(z, z + 1) == strChar){

                         //The character is contained in the string, value is now true

                             boChar = true;

                   }

               }              

               if (!boChar){

                   //If character is not contained in the string, the character is added to the string, otherwise it is not added

                   strPassword += strChar;

               }

          } else{

                    i--;

          }

}

Avatar

Level 2

Thanks very much for the explination - it makes sense.

I must be doing something wrong - here is my script with the "check for duplicate", but it will not fire.

var strPassword = "";

//Set up the length of the password

var intLength = 15;

for (var i = 0; i < intLength; i++){

          var random = Math.floor(Math.random() * 150);

          if ((random >= 48 && random <= 57) || (random >= 65 && random <= 90) || (random >=97 && random <= 122)){

               //Store character in value   

               var strChar = String.fromCharCode(random);

               //Character is initially not contained in the string

               var boChar = false;

               for (var z = 0; z < strValue.length; z++){

                   if (strValue.substr(z, z + 1) == strChar){

                         //The character is contained in the string, value is now true

                             boChar = true;

                   }

               }             

               if (!boChar){

                   //If character is not contained in the string, the character is added to the string, otherwise it is not added

                   strPassword += strChar;

               }

          } else{

                    i--;

          }

}

xfa.host.messageBox(strPassword);

Avatar

Level 10

Yea I made an error in the code, strValue is suppose to be strPassword

for (var z = 0; z < strPassword.length; z++){

                   if (strPassword.substr(z, z + 1) == strChar){

                         //The character is contained in the string, value is now true

                             boChar = true;

                   }

               }   

Avatar

Level 2

Perahps I spoke to soon - I am still getting duplicates in the response.

Below is my total script which populates a text filed on a button click.  -  Sorry to ping back on this, but it is very close.  any ideas?

var strPassword = "";

//Set up the length of the password

var intLength = 8;

for (var i = 0; i < intLength; i++){

          var random = Math.floor(Math.random() * 150);

          if ((random >= 35 && random <= 38) || (random == 33)|| (random == 42) || (random == 95) || (random >= 48 && random <= 57) || (random >= 63 && random <= 78)|| (random >= 80 && random <= 93) || (random >=97 && random <= 110) || (random >=112 && random <= 122)){ //Insert your password in the string var

               //Store character in value  

               var strChar = String.fromCharCode(random);

               //Character is initially not contained in the string

               var boChar = false;

               for (var z = 0; z < strPassword.length; z++){

                   if (strPassword.substr(z, z + 1) == strChar){

                         //The character is contained in the string, value is now true

                             boChar = true;

                   }

               }  

                        

               if (!boChar){

                   //If character is not contained in the string, the character is added to the string, otherwise it is not added

                   strPassword += strChar;

               }

          } else{

                    i--;

          }

}

strPasswordTxtNO.rawValue = strPassword;

Avatar

Correct answer by
Level 10

//strPassword.substr(), created some problems... somehow it could return more than one character at a time.... I don't know why but that was the problem for duplicates... so I changed it for charAt()

//Also sometimes it was possible you could only have 6 or 7 length instead of 8 all the time... I had to add (i--;) if boChar was true

//Now it should work perfectly fine!

var strPassword = "";

//Set up the length of the password

var intLength = 8;

for (var i = 0; i < intLength; i++){

          var random = Math.floor(Math.random() * 150);

          if ((random >= 35 && random <= 38) || (random == 33)|| (random == 42) || (random == 95) || (random >= 48 && random <= 57) || (random >= 63 && random <= 78)|| (random >= 80 && random <= 91) || (random == 93) || (random >=97 && random <= 110) || (random >=112 && random <= 122)){ //Insert your password in the string var

              //Store character in value 

        var strChar = String.fromCharCode(random);

        //Character is initially not contained in the string

        var boChar = false;

        for (var z = 0; z < strPassword.length; z++){

                  if (strPassword.charAt(z) == strChar){

                     //The character is contained in the string, value is now true

               boChar = true;

           }

        } 

        if (!boChar){

                  //If character is not contained in the string, the character is added to the string, otherwise it is not added

           strPassword += strChar;

        } else {

                              i--;

        }

          } else{

              i--;

    }

}

Avatar

Level 2

Perfect - thanks so much.  I hope this exercise will help others.