密码的正则表达式,包含至少2个大写字母,2个小写字母,2个符号和任意序列中的2个数字

时间:2017-02-25 07:37:17

标签: javascript angularjs

当我在RegEx()中使用它时,这在JavaScript密码验证中不起作用:

(?=(.*\\d){2,})(?=(.*[A-Z]){2,})(?=(.*[a-z]){2,})(?=(.*[!@#$%^&*?]){2,})(?!.*[\\s])^.*

1 个答案:

答案 0 :(得分:1)

你走了:
我使用的正则表达式是:

1)(?=(。 \ d){2}) - >至少2位数 2)(?=(。 [a-z]){2}) - >至少2个小写字母
3)(?=(。 [A-Z]){2}) - >至少2个小写字母
4)(?=(。
[!@#$%]){2}) - >至少2个特殊字符,可以是!,@,#,$,%

中的任何一个

angular.module('myApp', [])
  .controller('MyController', function($scope) {
    $scope.password = '';

    // (?=(.*\d){2}) --> atleast 2 digits
    // (?=(.*[a-z]){2}) --> atleast 2 lower case chars
    // (?=(.*[A-Z]){2}) --> atleast 2 lower case chars
    // (?=(.*[!@#$%]){2}) --> atleast 2 special chars, can be any one of !, @, #, $, %
    $scope.pattern = /(?=(.*\d){2})(?=(.*[a-z]){2})(?=(.*[A-Z]){2})(?=(.*[!@#$%]){2})/;
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="MyController">

  <form name="myForm">
    <label>Enter password: </label>
    <input type="password" name="password" ng-model="password" ng-pattern="pattern">
    <span style="color:red" class="error" ng-show="myForm.password.$error.pattern">Password is not valid, doesn't match the provided pattern.</span>
  </form>

</div>