这篇文章主要介绍了js基于正则表达式实现的密码强度验证功能,涉及javascript事件响应及基于正则的字符遍历、判断等相关操作技巧,对javascript感兴趣的朋友可以参考下本篇文章
本文实例讲述了JS基于正则表达式实现的密码强度验证功能。分享给大家供大家参考,具体如下:
具体代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>www.php.cn 本站</title>
</head>
<style type="text/css">
body {
background: #ccc;
}
label {
width: 40px;
display: inline-block;
}
span {
color: red;
}
.container {
margin: 100px auto;
width: 400px;
padding: 50px;
line-height: 40px;
border: 1px solid #999;
background: #efefef;
}
span {
margin-left: 30px;
font-size: 12px;
}
.wrong {
color: red
}
.right {
color: green;
}
.strengthLv0 {
height: 6px;
width: 120px;
border: 1px solid #ccc;
padding: 2px;
}
.strengthLv1 {
background: red;
height: 6px;
width: 40px;
border: 1px solid #ccc;
padding: 2px;
}
.strengthLv2 {
background: orange;
height: 6px;
width: 80px;
border: 1px solid #ccc;
padding: 2px;
}
.strengthLv3 {
background: green;
height: 6px;
width: 120px;
border: 1px solid #ccc;
padding: 2px;
}
</style>
<body>
<p class="container">
<label>密码</label>
<input type="text" id="inp1" maxlength="16">
<!--<input type="password" id="inp1" maxlength="16"/>-->
<p class="pass-wrap">
<em>密码强度:</em>
<em id="strength"></em>
<p id="strengthLevel" class="strengthLv0"></p>
</p>
</p>
<script>
var regEx = /^[1-9]\d{4,9}$/; //匹配qq号
//找人
var inp1 = document.getElementById("inp1");
var strength = document.getElementById("strength");
var strengthLevel = document.getElementById("strengthLevel");
var arr = ["", "低", "中", "高"];
inp1.onkeyup = function () {
var level = 0;
if (/[1-9]/.test(this.value)) {
level++;
}
if (/[a-z]/.test(this.value)) {
level++;
}
if (/[^a-z1-9]/.test(this.value)) {
level++
}
if (this.value.length < 6) {
level = 0;
}
strength.innerHTML = arr[level];
strengthLevel.className = "strengthLv" + level;
};
/* inp1.onkeyup = function () {
var level = 0;
if (/[1-9]/.test(this.value)) {
level++;
}
if (/[a-z]/.test(this.value)) {
level++
}
if (/[^a-z0-9]/.test(this.value)) {
level++
}
if (inp1.value.length < 6) {
level = 0;
}
strengthLevel.className = "strengthLv"+level;
strength.innerHTML = arr[level];
};*/
</script>
</body>
</html>以上就是本篇文章的所有内容,希望对大家学习提供到帮助!!
相关推荐:
立即学习“Java免费学习笔记(深入)”;
以上就是JS基于正则表达式实现的密码强度验证功能示例_javascript技巧的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号