使用JS验证密码的强度
来源:互联网 发布:数据录入口径 编辑:程序博客网 时间:2024/05/18 03:02
密码强度评分,根据得分显示密码强度。
function testpass(password,username){
var score = 0;
if (password.length < 4 ) { return -4; }
if (typeof(username) != 'undefined' && password.toLowerCase() == username.toLowerCase()){return -2}
score += password.length * 4;
score += ( repeat(1,password).length - password.length ) * 1;
score += ( repeat(2,password).length - password.length ) * 1;
score += ( repeat(3,password).length - password.length ) * 1;
score += ( repeat(4,password).length - password.length ) * 1;
if (password.match(/(.*[0-9].*[0-9].*[0-9])/)){ score += 5;}
if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)){ score += 5 ;}
if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)){ score += 10;}
if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)){ score += 15;}
if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/)){ score += 15;}
if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/)){score += 15;}
if (password.match(/^\w+$/) || password.match(/^\d+$/) ){ score -= 10;}
if ( score < 0 ){score = 0;}
if ( score > 100 ){ score = 100;}
return score;
function repeat(len,str){
var res = "";
for (var i = 0; i < str.length; i++ ){
var repeated = true;
for (var j = 0, max = str.length - i - len; j < len && j < max; j++){
repeated = repeated && (str.charAt(j + i) == str.charAt(j + i + len));
}
if (j < len) repeated = false;
if (repeated) {
i += len - 1;
repeated = false;
}else{
res += str.charAt(i);
}
}
return res;
}
}
通过上述函数可对密码进行评分验证。
举例:
function checkpass(pass){
var username = document.getElementById('username').value;
var score = testpass(pass.value,username);
var password_label = document.getElementById('password_label');
if(score == -4) {
password_label.innerHTML = '太短';
}else if(score == -2){
password_label.innerHTML = '与用户名相同';
}else{
var color = score < 34 ? '#edabab' : (score < 68 ? '#ede3ab' : '#d3edab');
var text = score < 34 ? '弱' : (score < 68 ? '一般' : '很好');
var width = score + '%';
password_label.innerHTML = "<span style='width:"+width+";display:block;overflow:hidden;height:20px;line-height:20px;background:"+color+";'>"+text+"</span>";
}
}
</script>
请输入用户名:<br>
<input type="text" class="inpt" name="username" style="width:160px" id="username" /><br>
请输入密码:<br>
<input type="password" class="inpt" style="width:160px" onkeyup="javascript:checkpass(this)" name="pass" id="pass" /><br>
<span id="password_label" style="width:160px;border:1px solid #F0F0F0"></span>
- 使用JS验证密码的强度
- JS验证密码的强度
- 使用JS验证密码的强度 jQuery验证框架
- [JS]验证密码强度
- js验证密码强度
- JS密码强度验证
- js密码强度验证
- js验证密码强度
- js密码强度验证
- JS验证密码强度
- JS密码强度验证
- 验证密码强度js
- 密码强度的验证!!!
- 密码强度的验证
- 密码强度的验证
- js验证密码安全强度
- JS验证密码强度插件
- js实现密码强度验证
- C++中#define的使用方法
- Python中time模块详解(转)
- oracle学习之索引
- xhtml+css排版,清理浮动整理汇总
- 关于PNG透明,IE6,PNG8,PNG24,PNG32
- 使用JS验证密码的强度
- php获取文件mime类型的几种方法
- 使用Jquery取得浏览器的宽度和高度
- css 在未知高度的容器内让内容垂直居中
- 几个常用的无缝背景图分享网,以后不必Photoshop切背景图了
- Java线程面试题Top50
- 自动填写网页表单
- 百度搜索,你画了好大一张饼,你还打算欺骗多少人呢?有图为证
- jQuery插件,图片切割效果,配合PHP可以实现图片切割和调整尺寸