欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

JS随机密码生成算法

时间:2022-02-10 13:50:17|栏目:JavaScript代码|点击:

本文实例为大家分享了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>

上一篇:JS在Canvas元素内绘制钟表

栏    目:JavaScript代码

下一篇:用javascrpt将指定网页保存为Excel的代码

本文标题:JS随机密码生成算法

本文地址:http://www.codeinn.net/misctech/192987.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有