Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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])^.*
like image 631
Sam Avatar asked Oct 20 '25 10:10

Sam


1 Answers

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>
like image 140
Gaurav Avatar answered Oct 22 '25 00:10

Gaurav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!