这次给大家带来js使用正则进行密码强度验证,js使用正则进行密码强度验证的注意事项有哪些,下面就是实战案例,一起来看一下。
具体代码如下:
html>
<meta>
<title>密码强度验证</title><style>
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><p>
<label>密码</label>
<input>
<!--<input type="password" id="inp1" maxlength="16"/>-->
</p><p>
<em>密码强度:</em>
<em></em>
</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>
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
Biome 是一个基于 Rust 的高性能工具链,适用于 JavaScript、TypeScript、JSX、JSON、CSS 和 GraphQL,它将格式化和 linting 统一到一个工具中。具有 97% 的 Prettier 兼容性以及超过 450 条 lint 规则,Biome 以显著更快的执行速度取代了 ESLint 和 Prettier。










