Unlocking protection: Confirming password strength and security with password indicator and regular expressions for special characters in angular through directive

I have developed an app for password validation using an AngularJS directive. The requirements for the password include at least one special character, one capital letter, one number, and a minimum length of 8 characters. Additionally, I have included a password strength bar.

If you encounter any errors or mistakes in the app, please feel free to correct them.

Thank you!

You can access the Plunkr Link here

Here is the HTML file:

<!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 should contain 1 special & capital letter, 1 numeric character <br> &nbsp; &nbsp; and be a minimum of 8 characters long.</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>  

And here is the 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 review this code and make necessary modifications if required. Thanks!

Answer №1

Let's talk about mistakes:

isSatisfied(newVal && /[A-z]/.test(newVal)) +

When using [A-z], it's important to note that it matches more than just English letters - it also includes characters like [, \, ], ^, _, and `. You can find more information in this post on Stack Overflow.

In

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

It is recommended to anchor the look-ahead for better performance:

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

Remember that {8,8} is the same as {8} - representing exactly 8 occurrences of the preceding subpattern. The following pattern is suggested:

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

Alternatively (if anchoring is not default, confirming through research):

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When attempting to compress JavaScript with uglify-js, an unexpected token error occurs with the symbol ($)

When attempting to compress Bootstrap 4 js file using uglify-js, I encountered an error. The error message reads as follows: "Parse error at src\bootstrap\alert.js:1,7 import $ from 'jquery' ERROR: Unexpected token: name ($)". Now I am ...

Is there a way to retrieve all IDs within an array of objects using a foreach loop and merge the data associated with each ID?

I am currently working on a TypeScript API where I have merged all the data from this API with some additional data obtained from another API function. I have provided a snippet of my code which adds data to the first index of an array. M ...

Issue with Jquery firing function during onunload event

I'm having trouble adding a listener to a form in order to trigger an ajax call when the user leaves it. I can't seem to identify any errors in Firefox and nothing is getting logged in the console, indicating that my code might be incorrect. As s ...

Issues with Angular-Formly: The onChange event is not triggering when clicking the modal button

When I try to trigger a modal on the onChange event of a custom checkbox in Formly, the modal appears but the buttons are not functional. Can anyone help me figure out what I'm doing wrong? { key: 'coordinatesToLocateSite&apo ...

Mastering the use of gl Scissor is essential for effectively implementing popups in your

I am attempting to implement a pop-up view on top of the current webGL view. My strategy is as follows: Whenever I need to display a popup, I create a scissorRect and begin rendering the popup scene onto it. I was hoping that the content of the previous s ...

Experimenting with NGXS selectors: A comprehensive guide

Hey there, I am currently utilizing the NGXS state management library in my application. I have a selector set up like this and everything seems to be functioning correctly. However, while testing the app, I encountered the following error message: "PrintI ...

effectively showcasing information in a datatable by converting JSON data into objects

I am looking for a way to display balance information in datatables, but I have not been able to find a solution yet. <!DOCTYPE html> <html lang="en"> <head> <title>Comment</title> <meta charset="u ...

Tips for combining HTML and JavaScript on a WordPress site

As a WordPress developer who is still learning the ropes, I have come across a challenge with embedding html and JavaScript onto a page. Currently, I am in the process of redesigning a company website and one of the tasks involves integrating a calculator ...

Where do the props in React come from?

In my quest to learn React. I perused w3school's guide, but alas, the specific case I seek was not found. Even Google failed me in this pursuit, leading me here to pose my question. Behold, the code in question: <FooComponent foo="bar"> {( ...

The jQuery date picker refuses to function correctly when I attempt to initialize it after an AJAX call

I am facing an issue with my jquery-ui datepicker not working within the document.ready function after making an ajax call. However, it works fine when I add it inside the ajax complete function. Can someone please provide assistance on what could be cau ...

What is the best way to determine the value of margin-left in inline CSS using jQuery?

Here is the code snippet I am working with: <div class="ongoing" style="width: +728px; margin-left: +12480px"> I need to extract the value of the margin-left property for scrolling purposes. The technique I used to retrieve the width value does no ...

Tips for handling tasks with javascript in mongodb

The Mongo database is set up with a sharding structure of 3 Shards named TestSharding. Additionally, the script for this configuration can be written in JavaScript. I am tasked with developing a program that identifies whether a file is in .json or .csv f ...

Unable to retrieve the field value from the Json Object

I have a JSON object that I need to parse and display in a data table, but I'm having trouble reading the contents of the object. Here is my JavaScript function: finalGrid: function(data){ console.log("Final Grid"); var strJson = JSON.strin ...

If the given response `resp` can be parsed as JSON, then the function `$

I was using this script to check if the server's response data is in JSON format: try { json = $.parseJSON(resp); } catch (error) { json = null; } if (json) { // } else { // } However, I noticed that it returns true when 'res ...

A guide on trimming content with v-if in Vue.js

Recently, I attempted to trim the response value within a Vue condition. I require this functionality to apply the condition when the value is null or empty. <li v-if="item[0].otrodl4.trim() == ''" class="progress-step"> ...

Basic game of tic tac toe using JavaScript and jQuery

Teaching myself the ropes of JavaScript/jQuery, I decided to dive into building a simple tic tac toe game. Following this example as a guide, I embarked on creating my own version. While most parts seem to be working smoothly, there's one problem that ...

Using BeautifulSoup to Retrieve JPEG from an Image Tag's Src Attribute

Struggling with scraping this webpage for personal use. I am having trouble extracting the thumbnails of each item on the page. Despite being able to see image tags containing the required .jpgs when using "inspect" to view the html DOM, I cannot find the ...

fetch the image data from the clipboard

Hey there! Is it possible to use JavaScript to retrieve an image from the system clipboard (Windows/Mac) and paste it into a website? ...

Transform the outcome of Request() into a variable

I'm currently working with the following code snippet: request('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Gamma Case', function (e, r, body){ var req_data = JSON.parse(body); conso ...

JavaScript implementation of the results sorting

I have 2 sql queries to calculate the turnover for each semester. Results from query 1: { "LRU": [ "RADOME", "RADOME", "ATSU", "MFC", "FWC", "Unspecified", "AZE", "ECP", "CMM", "ECP" ], "Client": [ 17346, ...