Home > Web Front-end > JS Tutorial > body text

How to randomly generate a certain number of passwords using javascript

韦小宝
Release: 2018-01-15 11:37:58
Original
1818 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 js sample code, which is of certain significance to your study or work. Reference learning value, friends who are interested in JavaScript, 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

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

The above is all the content of this article, I hope Provide help for everyone's learning.

Related recommendations:

Javascript converts the absolute path of the image into base64 encoding

Javascript implements a progress bar based on a timer Function example

Detailed explanation of multiple inheritance examples using JavaScript

The above is the detailed content of How to randomly generate a certain number of passwords using javascript. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!