Home  >  Article  >  Web Front-end  >  Tutorial on javascript randomly generating a certain number of password codes

Tutorial on javascript randomly generating a certain number of password codes

小云云
小云云Original
2018-02-03 13:30:151576browse

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 (- _ #); this article mainly introduces how to use javascript to randomly generate Regarding information about passwords with a certain number of digits, detailed sample codes are given in the article, which have certain reference and learning value for everyone's study or work. I hope it can help everyone.

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("");
 }

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));

Related recommendations:

vue implements the method of remembering passwords to cookies

Detailed explanation of jquery gesture password plug-in

Play with jQuery to realize the password strength prompt information function when registering as a member

The above is the detailed content of Tutorial on javascript randomly generating a certain number of password codes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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