JavaScript DHTML/Security/Generated Password

Материал из Web эксперт
Перейти к: навигация, поиск

Generated Password in JavaScript

   <source lang="html4strict">

<HTML>

 <HEAD>
   <TITLE>Password Generator</TITLE>
   
 </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">
           Generated Password:
<INPUT TYPE="text" NAME="passField" VALUE="" SIZE="40">
           # of chars
  <INPUT TYPE="text" NAME="charLen" VALUE="8" SIZE="3">
           <INPUT TYPE="checkbox" NAME="punc" CHECKED> No Punctuation Marks 
<INPUT TYPE="checkbox" NAME="rLen"> Random Length (6 - 12)
           
<INPUT TYPE="button" VALUE=" Generate Password " onClick="GeneratePassword()">
   </FORM>
 </BODY>

</HTML>

      </source>