Generated Password in JavaScript : Generated Password « Security « JavaScript DHTML






Generated Password in JavaScript

<HTML>
  <HEAD>
    <TITLE>Password Generator</TITLE>
    <!--
      CryptoMX Tools
      Copyright (C) 2004 - 2006 Derek Buitenhuis

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation; either version 2
      of the License, or (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    -->
  </head>
  <body>
    <SCRIPT LANGUAGE="JavaScript">
      function GeneratePassword() {

          if (parseInt(navigator.appVersion) <= 3) {
              alert("Sorry this only works in 4.0 browsers");
              return true;
          }
          var length=8;
          var sPassword = "";
          length = document.aForm.charLen.value;

          var noPunction = (document.aForm.punc.checked);
          var randomLength = (document.aForm.rLen.checked);

          if (randomLength) {
              length = Math.random();

              length = parseInt(length * 100);
              length = (length % 7) + 6
          }


          for (i=0; i < length; i++) {

              numI = getRandomNum();
              if (noPunction) { while (checkPunc(numI)) { numI = getRandomNum(); } }

              sPassword = sPassword + String.fromCharCode(numI);
          }

          document.aForm.passField.value = sPassword

          return true;
      }

    function getRandomNum() {

          // between 0 - 1
          var rndNum = Math.random()

          // rndNum from 0 - 1000
          rndNum = parseInt(rndNum * 1000);

          // rndNum from 33 - 127
          rndNum = (rndNum % 94) + 33;

          return rndNum;
      }

      function checkPunc(num) {

          if ((num >=33) && (num <=47)) { return true; }
          if ((num >=58) && (num <=64)) { return true; }
          if ((num >=91) && (num <=96)) { return true; }
          if ((num >=123) && (num <=126)) { return true; }

          return false;
      }
    </SCRIPT>
    <FORM NAME="aForm">
      <TABLE>
        <TR>
          <TD>
            Generated Password:<BR>
            <INPUT TYPE="text" NAME="passField" VALUE="" SIZE="40"><BR>
          </TD>
          <TD>
            # of chars<BR>
            &nbsp;&nbsp;<INPUT TYPE="text" NAME="charLen" VALUE="8" SIZE="3">
          </TD>
        </TR>
        <TR>
          <TD>
            <INPUT TYPE="checkbox" NAME="punc" CHECKED> No Punctuation Marks <BR>
            <INPUT TYPE="checkbox" NAME="rLen"> Random Length (6 - 12) <BR>
          </TD>
        </TR>
        <TR>
          <TD COLSPAN="2" ALIGN=CENTER>
            <br>
            <INPUT TYPE="button" VALUE=" Generate Password " onClick="GeneratePassword()">
          </TD>
        </TR>
      </TABLE>
    </FORM>
  </BODY>
</HTML>
           
       








Related examples in the same category