简体   繁体   English

使用指令使用密码栏和regExp特殊字符对密码进行验证

[英]Password Validation with password bar & regExp special characters in angular using directive

This app is working for me BUT if someone find any error/mistake then please correct it. 如果有人发现任何错误/错误,则此应用对我有用,但请更正。

I was create a small app for password validation using angular js directive. 我创建了一个小应用程序,用于使用angular js指令进行密码验证。 Where user can validate password that's required One Special & Capital character, and one num value with minimum length 8. i also created password strength bar with it. 用户可以在其中验证所需的密码,一个特殊字符和大写字母,以及一个最小长度为8的数字。我还用它创建了密码强度栏。

Thanks 谢谢

Here Plunkr Link my 在这里Plunkr Link my

Here is My HTML file : 这是我的HTML文件:

<!DOCTYPE html>
<html>
<head>
   <link data-require="bootstrap@*" data-semver="3.3.1" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
   <script data-require="jquery@*" data-semver="2.1.3" src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
   <script data-require="bootstrap@*" data-semver="3.3.1" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
   <script data-require="angular.js@*" data-semver="1.4.0-beta.3" src="https://code.angularjs.org/1.4.0-beta.3/angular.js"></script>
   <script src="passwordModule.js"></script>
   <link rel="stylesheet" href="style.css" />
</head>
<body>
    <div ng-app="passwordModule" ng-controller="pwdCtrl" class="container">
    <h2>Password Validation:</h2>
    <form name="form">
       <div class="form-group">
            <label>Password</label>
            <input type="text" name="password" id="password" ng-model="user.password" ng-model-options="{allowInvalid: true}" 
             pattern-validator="((?=.*\d)(?=.*[A-Z])(?=.*\W).{8,8})" class="form-control"/>
      </div>
      <span class="alert alert-error" ng-show="form.password.$error.passwordPattern">
         Password required 1 special & capital letter, 1 numeric letter <br> &nbsp; &nbsp; Required minimum 8 letter.</span> 

      <div class="form-group">
        <label>Password Strength</label>
        <password-strength ng-model="user.password"></password-strength>

        <label>Confirm Password</label>
        <input class="form-control" type = "text" name = "Confpassword" ng-model="user.cnfPwd" data-equal-to="password" >
        <div data-ng-show = "showmsg"> Password matched </div>
        <div data-ng-show = "hidemsg"> Password not matched </div>
     </div>
     <button class="btn btn-primary" type="button" ng-disabled = "disabledButton"> save </button> 
 </form>
 </div>
 <script type="text/javascript">
 </script>
 </body>
 </html>  

Here is My Controller File : 这是我的控制器文件:

var pwdModule = angular.module('passwordModule', []);
//Controller
pwdModule.controller('pwdCtrl', ['$scope',
function($scope) {
  // Initialise the password as hello
  $scope.user = {};
  $scope.showmsg = false;
  $scope.disabledButton = true;


  if($scope.user.password === undefined) {
    $scope.showmsg = false;
  } 

  $scope.$watch('user.cnfPwd', function(newVal, oldVal) {
  if(newVal !== undefined){
      $scope.hidemsg = true;
    }

    if(newVal === $scope.user.password && $scope.user.password !== undefined) {
      $scope.showmsg = true;
      $scope.disabledButton = false;
      $scope.hidemsg = false;
    } else {
      $scope.showmsg = false;
      $scope.disabledButton = true;
    }
  })
}
]);

 // Directive: Validate a regex pattern   
 pwdModule.directive('patternValidator', [
 function() {
  return {
    require: 'ngModel',
    restrict: 'A',
    link: function(scope, elem, attrs, ctrl) {
      ctrl.$parsers.unshift(function(viewValue) {

        var patt = new RegExp(attrs.patternValidator);

        var isValid = patt.test(viewValue);

        ctrl.$setValidity('passwordPattern', isValid);

        return viewValue;
      });
    }
  };
  }
 ]);

// Dircetive: Display strength bar
pwdModule.directive('passwordStrength', [
function() {
  return {
    require: 'ngModel',
    restrict: 'E',
    scope: {
      password: '=ngModel'
    },

    link: function(scope, elem, attrs, ctrl) {

      scope.$watch('password', function(newVal) {

        var strength = isSatisfied(newVal && newVal.length >= 8) +
                        isSatisfied(newVal && /[A-z]/.test(newVal)) +
                        isSatisfied(newVal && /(?=.*\W)/.test(newVal)) +
                        isSatisfied(newVal && /\d/.test(newVal));

        var style = '',
            percent= 0;

        switch (strength) {
          case 1: 
              style = 'danger';
              percent = 25;
            break;
          case 2: 
            style = 'warning';
            percent = 50;
          break;
          case 3: 
            style = 'warning';
            percent = 75;
          break;
          case 4: 
            style = 'success';
            percent = 100;
          break;
        }

        scope.style = style;
        scope.percent = percent;

        function isSatisfied(criteria) {
          return criteria ? 1 : 0;
        }
      }, true);
    },
    template: '<div class="progress">' +
      '<div class="progress-bar progress-bar-{{style}}" style="width: {{percent}}%"></div>' +
      '</div>'
  }
}
])

Please check this and if any modification needed then and on it. 请检查并检查是否需要修改。 Thanks 谢谢

Speaking about errors: 谈到错误:

isSatisfied(newVal && /[Az]/.test(newVal)) + isSatisfied(newVal && /[Az]/.test(newVal))+

Here, [Az] matches more than English letters, it also matches [ , \\ , ] , ^ , _ , and ` , see this SO post . 在这里, [Az]匹配的不仅是英文字母,还匹配[\\]^_` ,请参见此SO post

In

isSatisfied(newVal && /(?=.*\\W)/.test(newVal)) + isSatisfied(newVal && /(?=.*\\W)/.test(newVal))+

you should anchor the look-ahead to increase performance: 您应该锚定前瞻性以提高性能:

isSatisfied(newVal && /^(?=.*\W)/.test(newVal)) +
                       ^

Note that {8,8} is equivalent to {8} - exactly 8 occurrences of the preceding subpattern. 请注意, {8,8}等效于{8} -前一个子模式恰好出现8次。 Use 采用

pattern-validator="(?=.*\d)(?=.*[A-Z])(?=.*\W).{8}"

Or (if it is not anchored by default, can't find it anywhere): 或者(如果默认情况下未锚定,则无法在任何地方找到它):

pattern-validator="^(?=.*\d)(?=.*[A-Z])(?=.*\W).{8}$"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM