Home >Web Front-end >JS Tutorial >How to implement JavaScript to randomly generate a certain number of passwords
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(""); }
Use
to pass in the minimum number of digits and the maximum number of digits in a generated password to return a random passwordconsole.log(createPassword(8,15));
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!