<form action="">
输入密码:
<input type="text" size="10" data-bind="value:Password, valueUpdate: 'afterkeydown'">
<br>
密码强度:
<table cellspacing="0" cellpadding="1" bordercolor="#cccccc"
height="23">
<tr bgcolor="#eeeeee">
<tddata-bind="style: { backgroundColor: Lcolor }">弱</td>
<tddata-bind="style: { backgroundColor: Mcolor }">中</td>
<tddata-bind="style: { backgroundColor: Hcolor }">强</td>
</tr>
</table>
</form>
然后就OK,运行代码查看,一模一样的功能展示出来了。
如果去掉为验证而改善的代码,总代码肯定是比原有的方式少的。
完整版代码如下:
复制代码 代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript" src="https://knockoutjs.com/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="https://knockoutjs.com/js/jquery.tmpl.js"></script>
<script type="text/javascript" src="https://knockoutjs.com/js/knockout-1.2.1.js"></script>
</head>
<body>
<script type="text/javascript">
var Page = Page || {};
Page.Utility = Page.Utility || {};
Page.Utility.Registration = Page.Utility.Registration || {};
//获取密码强度
Page.Utility.Registration.getPasswordLevel =function (password) {
if (password ==null|| password =='')
return0;
if (password.length <=4)
return0; //密码太短
var Modes =0;
for (i =0; i < password.length; i++) {
Modes |= CharMode(password.charCodeAt(i));
}
return bitTotal(Modes);
//CharMode函数
function CharMode(iN) {
if (iN >=48&& iN <=57) //数字
return1;
if (iN >=65&& iN <=90) //大写字母
return2;
if (iN >=97&& iN <=122) //小写
return4;
else
return8; //特殊字符
}
//bitTotal函数
function bitTotal(num) {
modes =0;
for (i =0; i <4; i++) {
if (num &1) modes++;
num >>>=1;
}
return modes;
}
};
var viewModel = {
Password: ko.observable(""),
Ocolor: "#eeeeee"
};
viewModel.PasswordLevel = ko.dependentObservable(function () {
return Page.Utility.Registration.getPasswordLevel(this.Password());
}, viewModel);
viewModel.Lcolor = ko.dependentObservable(function () {
//根据密码强度判断第一个格显示的背景色
returnthis.PasswordLevel() ==0?this.Ocolor : (this.PasswordLevel() ==1?"#FF0000" : (this.PasswordLevel() ==2?"#FF9900" : "#33CC00"))
}, viewModel);
viewModel.Mcolor = ko.dependentObservable(function () {
//根据密码强度判断第二个格显示的背景色
returnthis.PasswordLevel() <2?this.Ocolor : (this.PasswordLevel() ==2?"#FF9900" : "#33CC00")
}, viewModel);
viewModel.Hcolor = ko.dependentObservable(function () {
//根据密码强度判断第二个格显示的背景色
returnthis.PasswordLevel() <3?this.Ocolor : "#33CC00"
}, viewModel);
$((function () {
ko.applyBindings(viewModel);
}));
</script>
<form action="">
输入密码:<input type="text" size="10" data-bind="value:Password, valueUpdate: 'afterkeydown'">
<br>
密码强度:
<table cellspacing="0" cellpadding="1" bordercolor="#cccccc"
height="23">
<tr bgcolor="#eeeeee">
<td data-bind="style: { backgroundColor: Lcolor }">
弱
</td>
<td data-bind="style: { backgroundColor: Mcolor }">
中
</td>
<td data-bind="style: { backgroundColor: Hcolor }">
强
</td>
</tr>
</table>
</form>
</body>
</html>
您可能感兴趣的文章: