Home > Web Front-end > JS Tutorial > How to implement JavaScript to randomly generate a certain number of passwords

How to implement JavaScript to randomly generate a certain number of passwords

黄舟
Release: 2017-09-23 10:23:40
Original
1321 people have browsed it

This article mainly introduces you to the relevant information on how to use javascript to randomly generate a certain number of passwords. The article gives detailed sample code, which has certain reference learning value for everyone's study or work. It is needed Friends, please follow the editor to learn together.

Preface

This article mainly introduces the relevant content about using javascript to randomly generate a certain number of passwords, and shares it for your reference and study. , not much to say below, let’s take a look at the detailed introduction.

Requirements

Randomly generate a certain number of passwords. There is a minimum number and a maximum number. It must contain numbers, uppercase and lowercase letters, and special characters such as (- _ #);

code


##

function createPassword(min,max) {
 //可以生成随机密码的相关数组
 var num = ["0","1","2","3","4","5","6","7","8","9"];
 var english = ["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"];
 var ENGLISH = ["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"];
 var special = ["-","_","#"];
 var config = num.concat(english).concat(ENGLISH).concat(special);
 //先放入一个必须存在的
 var arr = [];
 arr.push(getOne(num));
 arr.push(getOne(english));
 arr.push(getOne(ENGLISH));
 arr.push(getOne(special));

 //获取需要生成的长度
 var len = min + Math.floor(Math.random()*(max-min+1));

 for(var i=4; i<len; i++){
  //从数组里面抽出一个
  arr.push(config[Math.floor(Math.random()*config.length)]);
 }

 //乱序
 var newArr = [];
 for(var j=0; j<len; j++){
  newArr.push(arr.splice(Math.random()*arr.length,1)[0]);
 }

 //随机从数组中抽出一个数值
 function getOne(arr) {
  return arr[Math.floor(Math.random()*arr.length)];
 }

 return newArr.join("");
 }
Copy after login

Use

to pass in the minimum number of digits and the maximum number of digits in a generated password to return a random password


console.log(createPassword(8,15));
Copy after login

Summarize

The above is the detailed content of How to implement JavaScript to randomly generate a certain number of passwords. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template