• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 556
  • Last Modified:

php code to javascript - password generator format XXX-XXX-XXX-XXX

can some one please convert this to javascript please

/* Random Password generator.
http://www.phpfreaks.com/quickcode/Random_Password_Generator/56.php

We'll generate a random password for the
user and encrypt it, email it and then enter it into the db.

MNC-HU4-GPY-JGD

*/
//len + 1 so 11 = 12 returned
function makeRandomPassword2($len = 11,$upper = 1,$number = 1) {
  $salt = "";
  //$salt = "abcdefghijklmnopqrstuvwxyz";
  $uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $numbers   = "1234567890";
  if ($upper) $salt .= $uppercase;
    if ($number) $salt .= $numbers;
   
srand((double)microtime()*1000000);
$i = 0;
$i1 = 0;
while ($i <= $len) {
  $num = rand() % strlen($salt);
  $tmp = substr($salt, $num, 1);
  $pass = $pass . $tmp;
  $i++;
  $i1++;
  if ($i1 == 3)
  {
  $i1 = 0;
  if ($i >= $len)
  {
  //dont do anything
  }
  else
  {
  $pass = $pass . "-";
  }
 
 
  }
 
 
};
return $pass;
};

thank you in advance for any code and/or help you may provide
0
Johnny
Asked:
Johnny
1 Solution
 
UnTwstrCommented:
Not quite sure if this is the format you want, but it randomly assigns letters or numbers for a password.

<SCRIPT LANGUAGE="JavaScript">
//MNC-HU4-GPY-JGD

        var len = 11;
      var upper = 1;
      var number = 1;
        var salt = "";
        var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        var numbers   = "1234567890";
      
      while (salt.length <= len){
            i = Math.random() * 1 + 1;
            i = Math.round(i);
            k = Math.random();
            if (i == 1){
                  k = Math.round(k*uppercase.length);
                  salt = salt + uppercase.charAt(k);
            }
            else{
                  k = Math.round(k*numbers.length);
                  salt = salt + numbers.charAt(k);
            }
      }

      pass = "";
      for (i=0;i<=salt.length;i++){
            pass = pass + salt.charAt(i);
            if(((i+1) % 3 ==0) && (i+1 != salt.length)){
                  pass = pass + "-";
            }
      }
      document.write(pass);

</SCRIPT>
0
 
JohnnyAuthor Commented:
Thank you
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now