如何用 JavaScript 建立密碼生成器?


如今,密碼生成器隨處可見。如果沒有足夠強的密碼,網站通常不會讓你建立帳戶。我們將要執行的任務是如何用 JavaScript 建立一個密碼生成器。

讓我們深入瞭解這篇文章,以便更好地理解如何建立密碼生成器。為此,我們將使用 `Math.random()` 來建立隨機密碼。

使用 Math.random() 方法

JavaScript 函式 `Math.random()` 返回範圍為 [0,1)(包括 0,但不包括 1)的浮點型偽隨機數。然後可以調整此隨機數以適應所需的範圍。

語法

`Math.random()` 的語法如下:

Math.random()

為了更好地理解如何建立密碼生成器,讓我們看看下面的例子。

示例

在下面的例子中,我們使用 `Math.random()` 執行指令碼以生成隨機密碼,並將隨機密碼限制為 4 位數字。

<!DOCTYPE HTML>
<html>
   <body style="text-align:center;background-color:#EAFAF1;">
      <h3>
         Click to Generate Random Passwords
      </h3>
      <button onclick="changepassword()">
         Click Here
      </button>
      <br>
      <div>
         <p id="tutorial"></p>
      </div>
      <script>
         var element_down = document.getElementById("tutorial");
         function passwordgenerator() {
            var pass = '';
            var str = 'WelcomeToTheTutorialsPoint' +
            'qujzkexlxwlkcewxmxekhnexj@#$';
            for (let i = 1; i <= 4; i++) {
               var char = Math.floor(Math.random()
               * str.length + 1);
               pass += str.charAt(char)
            }
            return pass;
         }
         function changepassword() {
            element_down.innerHTML = passwordgenerator();
         }
      </script>
   </body>
</html>

指令碼執行後,將生成包含文字和點選按鈕的輸出。當用戶點選按鈕時,會發生一個事件,每次點選按鈕都會生成一個 4 位隨機密碼。

示例

考慮以下示例,在這裡我們使用 `Math.random()` 執行指令碼以根據使用者輸入的長度生成隨機密碼。

<!DOCTYPE HTML>
<html>
   <body style="text-align:center;background-color:#E8DAEF;">
      <input type="text" id="digitspassword" oninput="generatepassword()" placeholder="Use Single Digits(0-9)"/>
      <p id="tutorial"></p>
      <script>
      function generatepassword(){
         var inputlength=document.getElementById('digitspassword');
         var UserInput=inputlength.value.replace(/[^0-9.]/g, "");
         inputlength.value=UserInput;
         var Results=document.getElementById('tutorial');
         var text = "";
         var shuffle = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
         if(inputlength!==''){
            for( var i=0; i < inputlength.value; i++ ){
               text += shuffle.charAt(Math.floor(Math.random() * shuffle.length));
            }
            Results.innerHTML=text;
         }
      }
      </script>
   </body>
</html>

執行上述指令碼後,將彈出輸出視窗,顯示供使用者輸入的輸入欄位以及輸入欄位內的佔位符文字。當用戶輸入數字時,它將根據使用者輸入的數字生成隨機密碼。

示例

執行以下指令碼,觀察我們如何隨機生成密碼以及如何使用兩個按鈕,一個用於生成,另一個用於複製。

<!DOCTYPE HTML>
<html>
   <body>
      <style>
      #button {
         font-size: 18px;
         margin-top: 15px;
         width: 100px;
         height: 50px;
         text-align: center;
         background-color: #ABEBC6 ;
         display: flex;
         color: rgb(23, 32, 42);
         justify-content: center;
         align-items: center;
         cursor: pointer;
         border-radius: 5px;
      }
      </style>
      <h2>Generate Password Randomly</h2>
      <input type="text" name="" placeholder="Create password" id="tutorial" readonly>
      <table>
         <th><div id="button" onclick="generatepassword()">Generate</div></th>
         <th><a id="button" onclick="passwordcopy()">Copy</a></th>
      </table>
      <script>
         var password=document.getElementById("tutorial");
         function generatepassword() {
            var chars = "01239abcdefghijABCDEF45678GHIJKLMNOPQRSTUVWXYZklmnopqrstuvwxyz!@#$%^&*()";
            var passwordLength = 5;
            var password = "";
            for (var i = 0; i <= passwordLength; i++) {
               var randomNumber = Math.floor(Math.random() * chars.length);
               password += chars.substring(randomNumber, randomNumber +1);
            }
            document.getElementById("tutorial").value = password;
         }
         function passwordcopy() {
            var copyText = document.getElementById("tutorial");
            copyText.select();
            document.execCommand("copy");
         }
      </script>
   </body>
</html>

指令碼執行後,它將在網頁上生成包含文字欄位以及兩個按鈕(一個用於生成,另一個用於複製)的輸出。當用戶點選生成按鈕時,它將生成密碼;當用戶點選複製按鈕時,生成的密碼將被複制。

更新於:2023年4月21日

603 次瀏覽

啟動您的 職業生涯

完成課程獲得認證

開始學習
廣告