如何使用JavaScript添加密码强度计到输入字段?
我想在更改密码表中添加密码强度表。我想了解JavaScript如何进行这种可能的方法,以及如果密码必须至少包含由大写,小写,数字等组成的字符,那么我该
如何设置有参考吗?我对此很陌生,并且正在尝试学习。感谢任何帮助,谢谢。
/* Show Hide Password */
function showPassword(targetID) {
var pw = document.getElementById(targetID);
if (pw.type === "password") {
pw.type = "text";
} else {
pw.type = "password";
}
}
fieldset {
border: 0!important; margin: 0!important; padding: 5px!important;
}
/*Input fields*/
input.field-settings {
width: 100%;
border-radius: 4px!important;
border: 2px solid #efefef!important;
padding: 12px!important;
margin-bottom: 6px;
height: 40px;
}
input.field-settings:focus {
border: 2px solid #6FA4F2!important;
}
input.field-settings.disabled {
background: var(--e-global-color-2606bfd);
color: var(--e-global-color-43596cc);
}
label.t2 {
font-size: 14px!important;
line-height: 1.5em!important;
font-weight: 500!important;
margin-bottom: 6px!important;
display: block;
}
span.t4-light {
margin: 0 6px;
display: block;
font-style: italic;
}
/*Toggle Password*/
.togglePw { display: none; margin-bottom: 0;}
.togglePw + label:before {
content: "\f06e";
font: var(--fa-font-solid);
margin-bottom: 6px;
margin-left: -30px;
display: block;
color: #8C9099;
}
.togglePw:checked + label:before {
content: "\f070";
font: var(--fa-font-solid);
margin-bottom: 6px;
margin-left: -30px;
display: block;
color: #1E70EB;
}
.input-group { display: flex; align-items: center; }
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.4/css/all.css" integrity="sha384-DyZ88mC6Up2uqS4h/KRgHuoeGwBcD4Ng9SiP4dIRy0EXTlnuz47vAwmeGwVChigm" crossorigin="anonymous"/>
<fieldset>
<p class="">
<label class="t2" for="password_current">Enter the current password (leave blank to not change)</label>
<div class="input-group">
<input type="password" class="field-settings" name="password" id="password_current" autocomplete="off"/>
<input type="checkbox" class="togglePw" id="pw_current" onclick="showPassword('password_current')"/>
<label for="pw_current" class="fa"></label>
</div>
</p>
<p class="">
<label class="t2" for="password_1">Enter your new password</label>
<div class="input-group">
<input type="password" class="field-settings" name="password_1" id="password_1" autocomplete="off" />
<input type="checkbox" class="togglePw" id="pw_1" onclick="showPassword('password_1')"/>
<label for="pw_1" class="fa"></label>
</div>
</p>
<p class="">
<label class="t2" for="password_2">Repeat your new password</label>
<div class="input-group">
<input type="password" class="field-settings" name="password_2" id="password_2" autocomplete="off" />
<input type="checkbox" class="togglePw" id="pw_2" onclick="showPassword('password_2')"/>
<label for="pw_2" class="fa"></label>
</div>
</p>
</fieldset>
I would like to add a password strength meter to the change password form. I would like to understand how this is possible with javascript and how I can set if the password must contain at least a minimum of characters made up of uppercase, lowercase, numbers etc.
Can anyone help me understand how to do it or if there are any references? I am quite new to this and am trying to learn. I appreciate any help, thanks.
/* Show Hide Password */
function showPassword(targetID) {
var pw = document.getElementById(targetID);
if (pw.type === "password") {
pw.type = "text";
} else {
pw.type = "password";
}
}
fieldset {
border: 0!important; margin: 0!important; padding: 5px!important;
}
/*Input fields*/
input.field-settings {
width: 100%;
border-radius: 4px!important;
border: 2px solid #efefef!important;
padding: 12px!important;
margin-bottom: 6px;
height: 40px;
}
input.field-settings:focus {
border: 2px solid #6FA4F2!important;
}
input.field-settings.disabled {
background: var(--e-global-color-2606bfd);
color: var(--e-global-color-43596cc);
}
label.t2 {
font-size: 14px!important;
line-height: 1.5em!important;
font-weight: 500!important;
margin-bottom: 6px!important;
display: block;
}
span.t4-light {
margin: 0 6px;
display: block;
font-style: italic;
}
/*Toggle Password*/
.togglePw { display: none; margin-bottom: 0;}
.togglePw + label:before {
content: "\f06e";
font: var(--fa-font-solid);
margin-bottom: 6px;
margin-left: -30px;
display: block;
color: #8C9099;
}
.togglePw:checked + label:before {
content: "\f070";
font: var(--fa-font-solid);
margin-bottom: 6px;
margin-left: -30px;
display: block;
color: #1E70EB;
}
.input-group { display: flex; align-items: center; }
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.4/css/all.css" integrity="sha384-DyZ88mC6Up2uqS4h/KRgHuoeGwBcD4Ng9SiP4dIRy0EXTlnuz47vAwmeGwVChigm" crossorigin="anonymous"/>
<fieldset>
<p class="">
<label class="t2" for="password_current">Enter the current password (leave blank to not change)</label>
<div class="input-group">
<input type="password" class="field-settings" name="password" id="password_current" autocomplete="off"/>
<input type="checkbox" class="togglePw" id="pw_current" onclick="showPassword('password_current')"/>
<label for="pw_current" class="fa"></label>
</div>
</p>
<p class="">
<label class="t2" for="password_1">Enter your new password</label>
<div class="input-group">
<input type="password" class="field-settings" name="password_1" id="password_1" autocomplete="off" />
<input type="checkbox" class="togglePw" id="pw_1" onclick="showPassword('password_1')"/>
<label for="pw_1" class="fa"></label>
</div>
</p>
<p class="">
<label class="t2" for="password_2">Repeat your new password</label>
<div class="input-group">
<input type="password" class="field-settings" name="password_2" id="password_2" autocomplete="off" />
<input type="checkbox" class="togglePw" id="pw_2" onclick="showPassword('password_2')"/>
<label for="pw_2" class="fa"></label>
</div>
</p>
</fieldset>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在下面添加按钮,并在单击事件侦听器上添加功能,您可以检查字符串的长度,至少一个大写和特殊字符。如果您的病情已经错误。您也可以使用警报或吐司消息呈现错误消息。
注意:有关现成的功能,可在Google上找到。您的参考链接之一如下。
password_validator
快乐编码
you need to add button below this from and add functionality on click event listener you could check length of string, at least one upperCase and special char. if your condition gone false. you could render error message by using Alert or Toast message also.
Note: for ready-cooked functions are available at google. one of link for your reference are below.
password_validator
Happy coding
访问此链接,我希望它能帮助您
http://jsfiddle.net.net/9suwp/
Visit this link I hope it would help you
http://jsfiddle.net/9SUWP/