我正在尝试根据用户提供的选项创建密码生成器。我当前的脚本允许用户选择大写、小写、数字和特殊字符。这完美地工作并且字符串被生成到用户所需的长度,但是在生成时,数字聚集在字符串中,字母聚集在开头。一个特殊字符将两者分开。您对如何改进流程有什么建议吗?
$('document').ready(function() {
$('button').click(function() {
var lower = "";
var upper = "";
var numeric = "";
var special = "";
var string_length = "";
if($('#12').is(':checked')) { string_length = 12; };
if($('#16').is(':checked')) { string_length = 16; };
if($('#18').is(':checked')) { string_length = 18; };
if($('#22').is(':checked')) { string_length = 22; };
if($('#24').is(':checked')) { string_length = 24; };
if($('#custom').is(':checked')) { $('#custom').show(); $('#custom').val(); } else { $('#custom').hide(); };
if($('#ch1').is(':checked')) { lower = "abcdefghijklmnopqrstuvwxyz"; } else { lower = ""; };
if($('#ch2').is(':checked')) { upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; } else { upper = ""; };
if($('#ch3').is(':checked')) { numeric = "0123456789"; } else { numeric = ""; };
if($('#ch4').is(':checked')) { special = "!£$%^&*()_-+={};:@~#?/"; } else { special = ""; };
var chars = lower + upper + numeric + special;
var randomstring = '';
var charCount = 0;
var numCount = 0;
for (var i=0; i<string_length; i++) {
if((Math.floor(Math.random() * 2) == 0) && numCount < 3 || charCount >= 5) {
var rnum = Math.floor(Math.random() * 10);
randomstring += rnum;
numCount += 1;
} else {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
charCount += 1;
}
}
$('span.string').html(randomstring);
});
});
选项 16 长度、小写、大写、数字和特殊字符返回类似e046pzw%65760294的内容。