JS随机生成密码,同时包含大小写字母、数字、标点符号
浏览数 191629
赞
(0)
<script type="text/javascript">
/*
* 随机生成一个8-16位字符串的密码
* 同时包含大小写字母、数字、标点符号
*/
function randPassword()
{
var text=['abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ','1234567890','~!@#$%^&*()_+";",./?<>'];
var rand = function(min, max){return Math.floor(Math.max(min, Math.random() * (max+1)));}
var len = rand(8, 16); // 长度为8-16
var pw = '';
for(i=0; i<text.length; ++i) {
pw += text[i].charAt(rand(0, text[i].length)) ;
}
for(i=0; i<len-4; ++i) {
var strpos = rand(0, 3);
pw += text[strpos].charAt(rand(0, text[strpos].length));
}
// if(pw.search(/\d+/) == -1){
// pw += text[2].charAt(rand(0, text[2].length)) ;
// }
// if(pw.search(/[a-z]+/) == -1){
// pw += text[0].charAt(rand(0, text[0].length)) ;
// }
// if(pw.search(/[A-Z]+/) == -1){
// pw += text[1].charAt(rand(0, text[1].length)) ;
// }
// if(pw.search(/[^0-9a-zA-Z]+/) == -1){
// pw += text[3].charAt(rand(0, text[3].length)) ;
// }
console.log(pw );
pw = pw.split('').sort(function(a, b) {
//用Math.random()函数生成0~1之间的随机数与0.5比较,返回-1或1
return Math.random()>.5 ? -1 : 1;
});
pw = pw.join('');
console.log(pw );
return pw;
}
/*
* 随机生成一个8位字符串的密码
* 同时包含大小写字母、数字、标点符号
*/
function randPassword8()
{
var text=['abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ','1234567890','~!@#$%^&*()_+";",./?<>'];
var rand = function(min, max){return Math.floor(Math.max(min, Math.random() * (max+1)));}
var len = rand(8, 16); // 长度为8-16
var pw = [];
for(i=0; i<4; i++)
{
pw.push(text[i].charAt(rand(0, text[i].length)) );
pw.push(text[i].charAt(rand(0, text[i].length)) );
}
pw.sort(function(a, b) {
//用Math.random()函数生成0~1之间的随机数与0.5比较,返回-1或1
return Math.random()>.5 ? -1 : 1;
});
pw = pw.join('');
console.log(pw);
return pw;
}
randPassword();
randPassword8();
</script>
/*
* 随机生成一个8-16位字符串的密码
* 同时包含大小写字母、数字、标点符号
*/
function randPassword()
{
var text=['abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ','1234567890','~!@#$%^&*()_+";",./?<>'];
var rand = function(min, max){return Math.floor(Math.max(min, Math.random() * (max+1)));}
var len = rand(8, 16); // 长度为8-16
var pw = '';
for(i=0; i<text.length; ++i) {
pw += text[i].charAt(rand(0, text[i].length)) ;
}
for(i=0; i<len-4; ++i) {
var strpos = rand(0, 3);
pw += text[strpos].charAt(rand(0, text[strpos].length));
}
// if(pw.search(/\d+/) == -1){
// pw += text[2].charAt(rand(0, text[2].length)) ;
// }
// if(pw.search(/[a-z]+/) == -1){
// pw += text[0].charAt(rand(0, text[0].length)) ;
// }
// if(pw.search(/[A-Z]+/) == -1){
// pw += text[1].charAt(rand(0, text[1].length)) ;
// }
// if(pw.search(/[^0-9a-zA-Z]+/) == -1){
// pw += text[3].charAt(rand(0, text[3].length)) ;
// }
console.log(pw );
pw = pw.split('').sort(function(a, b) {
//用Math.random()函数生成0~1之间的随机数与0.5比较,返回-1或1
return Math.random()>.5 ? -1 : 1;
});
pw = pw.join('');
console.log(pw );
return pw;
}
/*
* 随机生成一个8位字符串的密码
* 同时包含大小写字母、数字、标点符号
*/
function randPassword8()
{
var text=['abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ','1234567890','~!@#$%^&*()_+";",./?<>'];
var rand = function(min, max){return Math.floor(Math.max(min, Math.random() * (max+1)));}
var len = rand(8, 16); // 长度为8-16
var pw = [];
for(i=0; i<4; i++)
{
pw.push(text[i].charAt(rand(0, text[i].length)) );
pw.push(text[i].charAt(rand(0, text[i].length)) );
}
pw.sort(function(a, b) {
//用Math.random()函数生成0~1之间的随机数与0.5比较,返回-1或1
return Math.random()>.5 ? -1 : 1;
});
pw = pw.join('');
console.log(pw);
return pw;
}
randPassword();
randPassword8();
</script>