JS随机密码生成算法

编程学习 2021-07-04 15:01www.dzhlxh.cn编程入门
这篇文章主要为大家详细介绍了JS随机密码生成算法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了JS生成随机密码的具体代码,供大家参考,具体内容如下

题目:

输入一个正整数n,生成n个随机密码,其中包括数字,大小写字母。

代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
  div{
    width: 300px;
    height: 100px;
    background-color: lightblue;
    overflow: auto;
  }
  </style>
</head>
<body>
  <input type="text">
  <button>生成</button>
  <div class="end"></div>
  <script>
  var oTn = document.getElementsByTagName("input")[0];
  var oBtn = document.getElementsByTagName("button")[0];
  var oDiv = document.querySelector(".end");
  var password = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o",
  "p","q","r","s","t","u","v","w","x","y","z","A","B","C","D",
  "E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S",
  "T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"
  ]; 
  function pass(length){
    var Str = "";
    for(var i = 0 ; i<length;i++){
      Str += password[Math.floor(Math.random()*password.length)];
    }
    return Str;
  }
  oBtn.onclick = function(){
    oDiv.innerHTML = pass(oTn.value);
  }
  </script>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板