Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

javascript - Regular expression for a password containing at least 2 uppercase letters, 2 lowercase letters, 2 symbols, and 2 numbers in any sequence

This is not working in JavaScript password validation when I am using it with RegEx():

(?=(.*\d){2,})(?=(.*[A-Z]){2,})(?=(.*[a-z]){2,})(?=(.*[!@#$%^&*?]){2,})(?!.*[\s])^.*
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here you go:
The regex that I've used is:

1) (?=(.d){2}) --> atleast 2 digits
2) (?=(.
[a-z]){2}) --> atleast 2 lower case chars
3) (?=(.[A-Z]){2}) --> atleast 2 upper case chars
4) (?=(.
[!@#$%]){2}) --> atleast 2 special chars, can be any one of !, @, #, $, %

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 upper 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...