AngularJS 实现判断密码

来源:互联网 发布:胡歌幽默知乎 编辑:程序博客网 时间:2024/06/15 06:27

输入密码判断密码强度

安全强度:*低

安全强度:***中

安全强度:*****高


1.定义css样式

 <style type="text/css">
    .show-green{
    color: green;
    }
    .show-yellow{
    color: orange;
    }
    .show-red{
    color: red;
    }
    </style>


2.将样式引入输入框下的文字显示标签中,并将部分字符绑定show-{{teColor}}
将输入框绑定键盘弹起事件ng-keyup="checkPassword(password)

<input type="password" class="form-control" name="password" id="inputaccount" ng-model="password" ng-keyup="checkPassword(password)" placeholder="6-16位数字或英文字母">
<p class="text-right grey show-{{teColor}}" ng-bind="safeMsg"></p>---------------------------文字显示


3.编写控制器中的方法

//输入密码  键盘弹起事件 判断密码强度
$scope.checkPassword = function(password){
//$scope.safeMsg = "安全强度:*";
//$scope.teColor = "red";

//正则表达式判断密码强度
var lowTest1 = /^\d{1,6}$/; //纯数字
var lowTest2 = /^[a-zA-Z]{1,6}$/; //纯字母
var halfTest = /^[A-Za-z0-9]{1,6}$/;
var halfTest2 = /^[A-Za-z0-9]{6,8}$/;
var highTest = /^[A-Za-z0-9]{6,16}$/;
if(lowTest1.test(password)|lowTest2.test(password)|halfTest.test(password)){
$scope.safeMsg = "安全强度:*低";
$scope.teColor = "red";
}else if(halfTest2.test(password)){
$scope.safeMsg = "安全强度:***中";
$scope.teColor = "yellow";
}else if(highTest.test(password)){

$scope.safeMsg = "安全强度:*****高";
$scope.teColor = "green";
$scope.repassDis = "false";
}else{
$scope.safeMsg = "密码不符合要求";
$scope.teColor = "red";
}

};