AngularJS - Regular Expression Error

I have implemented a RegEx pattern to validate passwords entered by users. The password must contain at least 1 capital letter, 1 number, and 1 symbol:

/(?=.*?\d)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[^a-zA-Z\d])/

When this RegEx is used with ng-pattern in an input field of type password, it works correctly for inputs like Mike123@ or Mike123*. However, an error is thrown when entering Mike123***:

SyntaxError: Invalid regular expression: /^Mik123***$/: Nothing to repeat

The RegEx functions properly here: https://regex101.com/r/j0O4aw/1. Here is the code snippet where the RegEx is applied:

<div class="input_fieldholder">
  <input type="password"
  class="m-b-0 login_typefield"
  placeholder="Password"
  name="password"
  ng-class="{ form_error: vm.formObject.password.$dirty && vm.formObject.password.$invalid }"
  ng-model="vm.password"
  ng-minlength="6"
  ng-change="vm.onChangePassword()"
  ng-pattern="/(?=.*?\d)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[^a-zA-Z\d])/"
  required
  ng-attr-autofocus="{{ vm.autoFocus }}">

  <div class="text-left m-t-3">
    <div ng-show="vm.formObject.password.$dirty && vm.formObject.password.$error.required" class="text-danger">{{ 'PASSWORD_REQUIRED_VALIDATION' | translate }}</div>
    <div ng-show="vm.formObject.password.$dirty && vm.formObject.password.$error.minlength" class="text-danger">{{ 'PASSWORD_LENGTH_VALIDATION' | translate }}</div>
    <div ng-show="vm.formObject.password.$dirty && vm.formObject.password.$error.pattern" class="text-danger">{{ 'PASSWORD_PATTERN_VALIDATION' | translate }}</div>
  </div>
</div>

<div class="input_fieldholder">
  <input type="password"
  class="m-t-10 m-b-0 login_typefield"
  placeholder="Confirm Password"
  name="confirmPassword"
  ng-class="{ form_error: vm.formObject.confirmPassword.$dirty && vm.formObject.confirmPassword.$invalid }"
  ng-model="vm.confirmPassword"
  ng-pattern="vm.password"
  ng-change="vm.onChangePassword()"
  required
  compare-field-selector="[name='password']">
  <div class="text-left m-t-3">
    <span ng-show="vm.formObject.confirmPassword.$dirty && vm.formObject.confirmPassword.$invalid" class="text-danger">{{'PASSWORD_CONFIRM_VALIDATION' | translate }}</span>
  </div>
</div>

I am looking for a solution to this issue. Can you provide any suggestions?

Answer №1

Here is an example for you to follow.

The “id=pattern” will hold the pattern that needs to be matched. The “id=input” will contain the text that will be checked against the pattern.

<script>
  angular.module('ngPatternExample', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.pattern = '\\w+';
  }]);
</script>

This code snippet should be followed by :

<div ng-controller="ExampleController">
 <form name="form">
  <label for="pattern">Enter a pattern (regex string): </label>
  <input type="text" ng-model="pattern" id="pattern" />
  <br>
  <label for="input">This input must match the defined pattern: 
  </label>
  <input type="text" ng-model="text" id="input" name="input" ng-pattern="pattern" /><br>
  <hr>
  Is input valid? = <code>{{form.input.$valid}}</code><br>
  Entered text: <code>{{text}}</code> 
 </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

Having trouble retrieving Bengali-language data from the server using jQuery AJAX

I am facing an issue where I am unable to fetch data in Bengali language from the server using ajax. Strangely, the data retrieved from the server is getting replaced by some unknown characters. However, if I directly retrieve the data without using ajax, ...

Node.js equivalent of Java's byteArray

I have a Node.js REST API with images being sent from a Java application as byte arrays. Here is an image Below is the string representation of the image in byte array form: [B@c75af72 I need to decode this byte array to verify if it is indeed an ima ...

Adjust image size based on the size of the screen

I am facing an issue with two images that are created for a 1024 x 768 resolution but I need to use them in a higher resolution. The challenge is to align these two images so that the circle from the first image falls between the second image. Although I ...

Passing a URL from a controller to a Directive in AngularJS can be done by using

angular.module('finalApp').controller('GetAgentDetailsCtrl', function ($scope,$rootScope){ var vm=this; vm.url='api/agent/getAgentDetails'; }); 'use strict'; angular.module('finalApp').directive(& ...

The persistent issue persists with the Sails.js Node server where the req.session remains devoid of any data, despite utilizing

Currently, I have an Ember app running on http://localhost:4200 and a Sails app running on http://localhost:1337. In order to set up a pre-signup survey policy in my Sails application, I have the following code in /api/controllers/ProcessSurveyController. ...

Can you display names in capital letters from the randomUser.me API?

Apologies if this is not the appropriate platform for my query. Just to provide context, I am a designer with minimal experience in APIs and Javascript. I am currently utilizing the randomUser API to create a JSON file or URL that can be integrated into I ...

Trouble with Bootstrap Modal not closing properly in Chrome and Safari

ISSUE: I'm facing a problem where clicking on the X (font awesome icon) doesn't close the modal popup as expected. https://i.sstatic.net/yXnwA.jpg LIMITED FUNCTIONALITY ON CERTAIN BROWSERS: Currently, the X button for closing the modal works o ...

Is it possible to utilize both the /app and /pages folders in my Next 13 application?

I am currently working on a project where the /app folder is utilized as the primary route container. However, we have encountered performance issues on localhost caused by memory leaks identified in an issue on the Next.js repository. I am curious to fi ...

Mix up the colors randomly

After searching extensively, I have been unable to find exactly what I am looking for. The closest matches only cover a portion of my needs which include: I am seeking a randomly selected color scheme that I have created but not yet implemented in code. T ...

The deployed app on Heroku is showing a 304 status code with no JSON response

I've been working on a project using React, Express, and MongoDB. While everything works fine locally, I encountered an issue with GET requests after deploying the project on heroku.com. Instead of receiving the expected JSON response, I'm gett ...

Attempting to fetch JSON information but encountering an error message stating "not a valid function."

I have been working on developing a programmer job board app and I am currently facing an issue with displaying JSON data on the main page. My goal is to eventually render the data, but for now, I just want to ensure that it appears in JSON format so that ...

React has been reported to display a Minified React error even in its development mode

Currently, I am utilizing browserify and babel for transpiling and bundling my script. However, a challenge arises when React 16 is incorporated as it presents the following error message: Uncaught Error: Minified React error #200; for more details, kin ...

What is the best way to simultaneously utilize two APIs where one is using HTTP and the other is using HTTPS?

What is the best way to simultaneously use two APIs, one being http and the other https, in Angular or JavaScript? ...

Scrolling presentation with dynamic animations

Does anyone have any suggestions for creating a scrolling effect similar to the one on this website? I want to implement a scroll effect where each presentation page is revealed one by one when the user scrolls using their mouse wheel, encouraging them to ...

Sliding in images with JQuery

I need help with animating the slide-in effect of 7 "card" images from the left to the center of the screen. I attempted to achieve this using the following code: function FetchCards() { $("#pack").css('margin-left', 0); $("#pack").css(& ...

How can we improve our handling of cyclic requires and EventEmitter in our code?

My user service code looks like this: 'use strict'; let events = require('services/events'); module.exports = { create: function (data) { doCreate(data).then(user => { events.emit('user.create'); ...

Send information to a different module

I've created a straightforward form component: <template> <div> <form @submit.prevent="addItem"> <input type="text" v-model="text"> <input type="hidden" v-model="id"> <i ...

Tool tips not displaying when the mouse is moved or hovered over

I have customized the code below to create a multi-line chart with tooltips and make the x-axis ordinal. However, the tooltips are not displaying when I hover over the data points, and I want to show the tooltips only for the data in the variable. https:/ ...

Discovering the RGB color of a pixel while hovering the mouse in MapboxGL Js

Looking to retrieve the RGB color value of the pixel under the mouse hover in MapboxGL Js. What is the most effective method to accomplish this task? ...

NodeJS module loading issue

Currently, I am attempting to utilize the resemblejs library () in order to compare two images. Despite creating a directory and adding resemblejs as a dependency, when running nodejs test.js, an error occurs: var api = resemble(fileData).onComplete(funct ...