gpt4 book ai didi

javascript - 密码生成器选择密码应包含的内容

转载 作者:行者123 更新时间:2023-11-30 11:44:04 24 4
gpt4 key购买 nike

我想改进我的密码生成器。我希望用户可以选择密码应包含的内容。例如,您可以选择密码只有字母但没有数字和字符。有人可以告诉我我必须做什么吗?

这是我的 javascript 和 html:-

  function randomPassword(length) {
var chars = "abcdefghijklmnopqrstuvwxyz!@#$%^&*()-+<>ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
var pass = " ";
for (var x = 0; x < length; x++) {
var i = Math.floor(Math.random() * chars.length);
pass += chars.charAt(i);
}
return pass;
}
function generate() {
myform.row_password.value = randomPassword(myform.length.value);
}
<form name="myform" method="post" class="form-horizontal">
<div class="form-group " >
<div class="col-sm-10">
<br>
<input class="rowpassword" type="text" name="row_password" size="45s">
<br>
<input class="form-control passwordlength" type="text" name="length" value="8" > password length
<br>
<br>
<input type="checkbox"> Groß - und Kleinbuchstaben
<br>
<input type="checkbox"> numbers
<br>
<input type="checkbox"> specialcharacters
<br>
<br>
<input type="button" class="form-control button" value="Passwort generieren" onClick="generate();" tabindex="2">
</div>
</div>
</form>


最佳答案

首先,检查复选框的 checked 属性。然后您可以决定是否拥有多个字符池,或者根据检查的内容应用过滤器。对于前者,您可以将适当的池添加在一起,所以类似于

var chars ="";
var letterPool= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var specialPool="!@#$%^&*()-+<>"
var numberPool="1234567890"

if(characterType.checked)
{
chars+=characterTypePool;
}

等如果您这样做,请不要忘记在没有选择时进行错误处理。

更新:我添加了一个片段(减去错误处理)来演示(不要忘记选中这些框!)

 
function randomPassword(length) {
var chars ="";
var letterPool= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var specialPool="!@#$%^&*()-+<>";
var numberPool="1234567890";

if(document.getElementById('letters').checked)
{
chars+=letterPool;
}

if(document.getElementById('numbers').checked)
{
chars+=numberPool;
}

if(document.getElementById('speChars').checked)
{
chars+=specialPool;
}

var pass = " ";
for (var x = 0; x < length; x++) {
var i = Math.floor(Math.random() * chars.length);
pass += chars.charAt(i);
}
return pass;
}
function generate() {
myform.row_password.value = randomPassword(myform.length.value);
}
<form name="myform" method="post" class="form-horizontal">
<div class="form-group " >
<div class="col-sm-10">
<br>
<input class="rowpassword" type="text" name="row_password" size="45s">
<br>
<input class="form-control passwordlength" type="text" name="length" value="8" > password length
<br>
<br>
<input type="checkbox" id='letters'> Groß - und Kleinbuchstaben
<br>
<input type="checkbox" id='numbers'> numbers
<br>
<input type="checkbox" id='speChars'> specialcharacters
<br>
<br>
<input type="button" class="form-control button" value="Passwort generieren" onClick="generate();" tabindex="2">
</div>
</div>
</form>

更新二:

要保证每种选定的字符类型至少有一种,您需要添加代码。一种方法是在开始时选择一个,并将其添加到最终密码中的随机位置。代码的以下改编通过使用数组和拼接来实现:

function randomPassword(length) {
var chars ="";
var letterPool= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var specialPool="!@#$%^&*()-+<>";
var numberPool="1234567890";
var guaranteed="";

if(document.getElementById('letters').checked)
{
chars+=letterPool;
guaranteed+=letterPool.charAt(Math.floor(Math.random() * letterPool.length));
}

if(document.getElementById('numbers').checked)
{
chars+=numberPool;
guaranteed+=numberPool.charAt(Math.floor(Math.random() * numberPool.length));
}

if(document.getElementById('speChars').checked)
{
chars+=specialPool;
guaranteed+=specialPool.charAt(Math.floor(Math.random() * specialPool.length));
}

var pass =[];
for (var x = 0,len=length-guaranteed.length; x < len; x++) {
var i = Math.floor(Math.random() * chars.length);
pass.push(chars.charAt(i));
}
for(var x = 0,len=guaranteed.length; x < len; x++)
pass.splice(Math.floor(Math.random() * pass.length),0,guaranteed.charAt(x));
pass=pass.join('');
return pass;
}
function generate() {
myform.row_password.value = randomPassword(myform.length.value);
}

关于javascript - 密码生成器选择密码应包含的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41594254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com