A password must contain a minimum of 2 uppercase letters, 2 lowercase letters, 2 symbols, and 2 numbers in any order according to the regular expression

I'm having trouble implementing this RegEx pattern in JavaScript for password validation:

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

Answer №1

Here is the regex pattern I used to validate passwords:

1) (?=(.\d){2}) --> Requires at least 2 digits
2) (?=(.
[a-z]){2}) --> Requires at least 2 lowercase letters
3) (?=(.[A-Z]){2}) --> Requires at least 2 uppercase letters
4) (?=(.
[!@#$%]){2}) --> Requires at least 2 special characters, such as !, @, #, $, %

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

    // (?=(.*\d){2}) --> Requires at least 2 digits
    // (?=(.*[a-z]){2}) --> Requires at least 2 lowercase letters
    // (?=(.*[A-Z]){2}) --> Requires at least 2 uppercase letters
    // (?=(.*[!@#$%]){2}) --> Requires at least 2 special characters, such as !, @, #, $, %
    $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, it does not match the required pattern.</span>
  </form>

</div>

Answer №2

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

    // (?=(.*\d){2}) --> at least 2 digits
    // (?=(.*[a-z]){2}) --> at least 2 lowercase characters
    // (?=(.*[A-Z]){2}) --> at least 2 uppercase characters
    // (?=(.*[!@#$%]){2}) --> at least 2 special characters, can be any 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, does not match the provided pattern.</span>
  </form>

</div>

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

CORS has restricted access to the XMLHttpRequest, despite the backend being configured correctly

I have a Flask backend integrated with React frontend. I encountered an issue while attempting to make a POST request to my backend. The error message states: Access to XMLHttpRequest at 'http://127.0.0.1:5000/predict' from origin 'http://lo ...

Discover unique values for a specified JSON attribute among all records in a database

Is it possible to fetch all the unique values of a json-property from multiple documents in a collection using the Marklogic Java Client API? For example, if there are 3 "MyDocument" type documents with the property "myProperty" as follows: MyDocument1.j ...

I am interested in creating a table that can toggle between show/hide mode with a plus/minus feature

$(document).ready(function() { $("#t1").hide(); // hide table by default $("#close").hide(); //hide the minus button as well if you only want one button to display at a time $('#sp1').on('click', function() { //when p ...

What is the best way to implement JavaScript for loading and removing content based on button clicks on a webpage?

I'm in need of a vanilla JavaScript solution (I know JQuery options exist, but I want to stick to vanilla JS for now)? Currently, I am using a simple page as a testing ground for my ongoing project. The page consists of two buttons that load HTML pag ...

Tips for utilizing import alongside require in Javascript/Typescript

In my file named index.ts, I have the following code snippet: const start = () => {...} Now, in another file called app.ts, the code is as follows: const dotenv = require('dotenv'); dotenv.config(); const express = require('express' ...

How come the method $.when().pipe().then() is functioning properly while $.when().then().then() is not working as expected

I'm still grappling with the concept of using JQuery's Deferred objects, and am faced with a puzzling issue. In this code snippet, my attempt to chain deferred.then() was unsuccessful as all three functions executed simultaneously. It wasn't ...

Checking authorization with CognitoUser solely for "code" provided by CognitoUser (npm: amazon-cognito-identity-js)

Currently, I am in the process of implementing the "forget password" feature. However, I have encountered an issue where I would like to verify the "verification code" first before setting a new "password". Unfortunately, the system currently requires bo ...

Hide the Modal Content using JavaScript initially, and only reveal it once the Onclick Button is activated. Upon clicking the button, the Modal should then be displayed to

While trying to complete this assignment, I initially attempted to search for JavaScript code that would work. Unfortunately, my first submission resulted in messing up the bootstrap code provided by the professors. They specifically requested us to use Ja ...

Is the detailedDescription missing from the JSON-LD schema crawl?

I am currently utilizing the Google Knowledge Graph Search (kgsearch) API to retrieve Schemas from schema.org. However, I am encountering an issue where some nested elements are not being recognized as JSON or I may be overlooking something... url = "http ...

Is it possible to showcase D3 charts on an .epub file?

For my research project, I am exploring the possibilities of .epub files and experimenting with embedding JavaScript code to display data visualizations. I am currently using calibre to convert an HTML file containing D3 scatterplots into an .epub. The s ...

Node.js encountered an error: TypeError - req.end does not have a function

Encountering an error stating that req.end is not a function, even though it works elsewhere in the code. Upon researching, some sources suggest that the global req variable might have been misplaced or lost. However, I haven't used any other variabl ...

Using Chrome.downloads.download within a Chrome application

My app is running into an issue where Chrome is giving me the error message that chrome.downloads is undefined. In my attempt to download an image, here is a simple example provided... Here is the manifest: { "manifest_version": 2, "name": "Download ...

How can it be that "Function" actually functions as a function?

In JavaScript, there exists a function called "Function". When you create an instance of this function, it returns another function: var myfunc = new Function('arg1','arg2','return arg1+arg2'); In the above example, the vari ...

prismjs plugin for highlighting code displays code in a horizontal format

If you're looking for a way to showcase source code on your website with highlighted syntax, prismjs.com may be just what you need. It offers a similar style to monokai... However, I've encountered an issue where the plugin displays my code in a ...

Error: Unable to execute decodeHtml because it is not recognized as a function

After transitioning to VueJS 2, I encountered a challenge. While using a filter that calls a custom function, I received the error message: TypeError: this.decodeHtml is not a function Below is my code snippet: new Vue({ el: '#modal' ...

Implementing file uploads in an Ionic application with a Web API

My dilemma is as follows: I am working with a WEB API where I need to upload images of boards. What do I need to do? Allow the user to select an image from their phone Enable the user to add a name for the board When the user clicks submit, the entered ...

storing data in local storage using JavaScript

As someone new to coding, my aim is to create a basic "daily planner" page where users can input text on different lines. I've been attempting to use local storage for this task, but seem to be running into some issues. I've experimented with get ...

Local email.js functionality successful, but fails upon deployment alongside React

I have implemented Email.js to create a contact form for a Next.js website. It functions perfectly when tested locally, but encounters issues once deployed. Upon clicking the submit button, the form fails to reset as intended within the sendEmail function. ...

Syncing data with angular-data is a straightforward process that can be easily

I'm currently working on an Angular project that utilizes angular-data to interact with my Restful API. Here's the situation: Within my service, I return a defined resource. In my controller, I use service.defineresource.findAll().then Everyth ...

CF component not able to accept arguments when invoked by JavaScript through cfajaxproxy

Ever since updating to ColdFusion 2016 Update 4, I've been experiencing a new issue. Here's the code snippet: <input type='button' name='btn' value='Click me' onclick='proxyFunc();'> Incorporating ...