Troubleshooting: Inconsistencies with AngularJS' ngmaxlength and ng-required

Input Type

<div class="form-group" ng-class="{ 'has-error': loginForm.password.$dirty && loginForm.password.$error.required }">
    <label for="password">{{ 'PASSWORD' | translate }} <span class="required-field"> *</span></label>
    <input uib-tooltip="{{ 'PLEASE_ENTER_PASSWORD' | translate }}" tooltip-placement="right" data-trigger="hover" type="password"
    name="password" id="password" class="color-tooltip form-control" ng-model="password" ng-maxlength="5" required />
    <span ng-show="loginForm.password.$dirty && loginForm.password.$error.required" class="help-block">{{ 'PASSWORDISREQUIRED' | translate }}</span>
    <span ng-if="!loginForm.password.$valid" class="help-block">The maximum length for password is 5 characters</span>
</div>

Using ng-maxlength in conjunction with required or ng-required, the error message appears for character limit even when input is empty. It should only display the error message when the input exceeds the specified character count.

Answer №1

Modify the condition to

!loginForm.password.$valid && loginForm.password.$error.maxlength

<div class="form-group" ng-class="{ 'has-error': loginForm.password.$dirty && loginForm.password.$error.required }">
  <label for="password">{{ 'PASSWORD' | translate }} <span class="required-field"> *</span></label>
  <input uib-tooltip="{{ 'PLEASE_ENTER_PASSWORD' | translate }}" tooltip-placement="right" data-trigger="hover" type="password" name="password" id="password" class="color-tooltip form-control" ng-model="password" ng-maxlength="5" required />
  <span ng-show="loginForm.password.$dirty && loginForm.password.$error.required" class="help-block">{{ 'PASSWORDISREQUIRED' | translate }}</span>
  <span ng-if="!loginForm.password.$valid && loginForm.password.$error.maxlength" class="help-block">The maximum length allowed for the password is 5 characters</span>
</div>

This will only display the error message if the condition specified is met, which in this case relates to the maximum character limit of the password field.

For illustration purposes:

var app = angular.module("my-app", []);
app.controller("my-controller", ["$scope", function($scope) {
  $scope.loginForm = null;
  $scope.password = "";
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="my-app" ng-controller="my-controller" ng-form="loginForm">
  <div class="form-group" ng-class="{ 'has-error': loginForm.password.$dirty && loginForm.password.$error.required }">
    <label for="password">Password <span class="required-field"> *</span></label>
    <input data-trigger="hover" type="password" name="password" id="password" class="color-tooltip form-control" ng-model="password" ng-maxlength="5" required />
    <span ng-show="loginForm.password.$dirty && loginForm.password.$error.required" class="help-block">Password is required</span>
    <span ng-if="!loginForm.password.$valid && loginForm.password.$error.maxlength" class="help-block">The maximum length allowed for the password is 5 characters</span>
  </div>
</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

Encountering a Next.js prerendering issue when using getStaticPaths

I am currently developing a Next.js application. Here is the structure of my files: cpanearme -components -listitem.js -pages -home -index.js -firm -[id].js Below is the code for a list item that redirects to the dynamic rout ...

Tips for minimizing the need to copy the entire dojo library each time you create a new Java EE package for deployment on GlassFish

Currently, I am diving into comet programming and utilizing the cometd implementation along with the JavaScript dojo library before deploying my war files to GlassFish. The issue I have encountered is that every time I create a new project, I find myself i ...

Ways to obtain the index of a button

Once upon a time, I used to create a plethora of buttons using a for loop. The title[] array was filled with numerous values. export const renderButtons1 = (numOfBtns,title,navigate) => { const views1 = []; for ( var i = 0; i < numOfBtns; ...

Error: Client was unable to process JSON data

My server setup looks like this: var http = require('http'); //Defining the port to listen to const PORT=8092; //Function that handles requests and sends responses function handleRequest(request, response){ response.statusCode = 200; ...

Cease the generation of dynamically produced sounds

I am encountering an issue in Angular where I am unable to stop playing an audio from a service. Below is my play() method: play(item: string): void { const audio = new Audio(); audio.src = item; audio.load(); audio.play(); } In order to stop all ...

The use of JavaScript within the code results in the <header> and <nav> elements failing to display on the browser

Currently, I am tackling a webpage project that involves using scripts in both the head and section elements to generate a table where clicking on the cells changes their color randomly. However, a glitch seems to be hindering the display of the header and ...

Utilizing ion-slide-box within an ion-content container that allows for scrolling

I've created an Ionic view with the following structure: <ion-content scroll="true"> <ion-list> ... some ion items... <ion-item> <ion-slide-box> <ion-slide ng-repeat="image i ...

Prevent the opening of tabs in selenium using Node.js

Currently, I am using the selenium webdriver for node.js and loading an extension. The loading of the extension goes smoothly; however, when I run my project, it directs to the desired page but immediately the extension opens a new tab with a message (Than ...

The formatting in the svg rendering from the object is not displaying correctly

I am working with a set of SVGs stored in an object: icon: { facebook: `<svg class="w-6 h-6 fill-current" viewBox="0 0 30 30" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M30 ...

A guide on using jQuery to iterate through 3 separate div elements

Seeking assistance with creating a dynamic loop for multiple divs (3 or more) using jQuery. The desired outcome is to have the main image div on the homepage rotate with other divs, changing both the background image and links within each div. I initially ...

How can you automate clicking a button and then performing a specific action through code?

In my current coding project, I am attempting to automate the process of clicking on a tab through a script in order to reveal additional divs on the webpage. Once these divs are revealed, I need to perform certain actions on them. My code currently looks ...

Is this code in line with commonly accepted norms and standards in Javascript and HTML?

Check out this Javascript Quiz script I created: /* Jane Doe. 2022. */ var Questions = [ { Question: "What is 5+2?", Values: ["7", "9", "10", "6"], Answer: 1 }, { Question: "What is the square root of 16?", Values: ["7", "5", "4", "1"], Answer: ...

Clarification: Javascript to Toggle Visibility of Divs

There was a similar question that partially solved my issue, but I'm wondering if using class or id NAMES instead of ul li - such as .menu, #menu, etc. would work in this scenario. CSS: div { display:none; background:red; width:200px; height:200px; } ...

How can we stop the page from scrolling after a click?

Upon clicking the link labeled Click Me, the page automatically scrolls back to the top. This behavior is not desired. How can I correct this issue? For example, here is a link to a relevant resource: http://jsfiddle.net/Y6Y3Z/ Regarding the scroll-bar: ...

Troubleshooting: JSColor Is Failing to Function Properly in Volusion's

Recently, I've encountered some issues with the JSColor plugin () on the Volusion e-commerce platform. Despite having successfully used it before, getting it to load correctly seems to be a challenge. My current task involves working on a test produc ...

Why is the fog in three.js not showing up?

Having trouble getting fog to render in my scene - any suggestions? I've scoured the internet for solutions, but can't seem to find any that work. Check out this screenshot to see the issue: https://i.sstatic.net/ThJRh.png Here is the code sni ...

Leveraging ASP.NET MVC 5 to integrate an online document viewer from Office 365, seamlessly displaying Word documents within a sleek, compact window

We have been struggling to showcase a Word document (.docx) within an iframe on our website using the Office 365 service. The document is stored in One-Drive for business online and has been appropriately shared. After signing in, we obtained a link to the ...

What is the best way to perform calculations within a PHP loop for <input> elements and then display the results using a JavaScript loop?

Hello everyone, I'm currently struggling with displaying the calculations from a loop of input tags. What I'm trying to achieve is having 5 rows with input fields. At the end of each row, there should be a span area that displays the calculation ...

Simulated external prerequisite

//user.js const database = require('database'); exports.createUser = function(req, res){ let user = req.body; if( validateUser(user) ) { database.insertUser(user); //redirect } else { //render new page with the user data ...

Compact looped slideshow

Currently in the process of designing a website and looking to incorporate a unique photo gallery feature. The concept is as follows: The photo gallery will be displayed in a rectangular/box shape. There are a total of 10 photos, with only 7 initially vis ...