Ways to display the length of text even when it exceeds the ng-maxlength limit

I am facing an issue where I cannot see the character count when the ng-maxlength limit is reached.

var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="plunker" ng-controller="MainCtrl">
  <form name="form" novalidate>
    <input type="text" ng-model="myChar" ng-maxlength="5">
    <br>
    <h1>Length: {{myChar.length}}</h1>
  </form>
</div>

Answer №1

Once the maxlength limit is reached, the $modelValue of myChar becomes undefined. However, you can still access the $viewValue property to get the desired result.

var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
  $scope.test = function() {
    console.log($scope.form.myChar.$viewValue.length);  
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<div ng-app="plunker" ng-controller="MainCtrl">
  <form name="form" novalidate>
    <input type="text" name="myChar" ng-model="myChar" ng-maxlength="5" ng-keyup="test()">
    <br>
    <h1>Length: {{myChar.length}}</h1>
  </form>
</div>

Answer №2

Here is an improved version of @John Smith's answer:

To enhance the verification process, you can directly integrate it into your HTML code. By applying CSS styling, you can visually indicate to the user when the input length is invalid:

var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<style>
.red {
color: red;
}
</style>
<div ng-app="plunker" ng-controller="MainCtrl">
  <form name="form" novalidate>
    <input type="text" name="myChar" ng-model="myChar" ng-maxlength="5">
    <br>
    <h1 ng-class="{'red': form.myChar.$viewValue.length > 5}">Length: {{form.myChar.$viewValue.length}}</h1>
  </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

TypeError: "Table" has not been declared

This is my first experience with the script editor. I have been given the task of creating a pivot table for Google Sheets using a script. // This script creates a pivot table in Google Sheets function createPivotTable() { var ss = SpreadsheetApp.getAc ...

Node.js is great at hosting HTML files, but struggles when it comes to loading script files within those pages

Recently, I’ve been working on creating a simple login page using a mongoDB database along with Node.js and express. Since I'm still new to Node.js, I'm facing an issue that seems more complicated than it actually is. The main problem I’m en ...

Using JavaScript to Detect Asynchronous Postbacks in ASP.NET AJAX

Seeking advice on the JavaScript code required to determine if an asynchronous postback is in progress. Can anyone help with this? Appreciate any assistance. ...

Alter the DOM element every ten seconds

Is there a way to modify a DOM element every 10 seconds? I attempted the following approach: var endurance = angular.element('.progressbar').height(); var endurance2 = angular.element('.progressbar').css('height', endurance- ...

Using either jQueryUI or BlockUI modal, you can create a dialog that covers only a specific

Is there a way to have a modal dialog specifically over a certain div container on a webpage? I've been searching for answers but haven't found anything regarding this with jQueryUI or BlockUI. Would modifying an existing overlay solution be the ...

Adjusting the view of the three.js camera

After exploring the Three.js globe example on github, I set out to create my own globe using some specific data. However, I am encountering difficulty in making the camera focus on a given coordinate. I attempted to use a function to convert coordinates i ...

When using Material UI, I ran into an issue where my Card Component was being added to my Appbar. To improve user interaction, I desire for the cards to only appear once

The formatting of the naming conventions is incorrect. Please disregard those. Snippet of code for the Appbar: const useStyles = makeStyles((theme) => ({ root: { flexGrow: 1 }, title: { flexGrow: 1 } })); export default function Appp() ...

Revamping elements according to ordered array. Angular version 4.3

Dealing with an array of data that needs to be sorted for displaying in a component seems to be a challenge. Despite having a functional code sample demonstrating the concept, the sorting is not reflected in the Angular app's DOM. The original data i ...

Unexpected behavior encountered in Rails app with unobtrusive JavaScript

I'm facing an issue with my link_to setup. Here's what I have: <%= link_to "Load More", comments_feed_path(@post.id), :id => 'my-link', :remote => true %> In my application.js file, I have the following code: $( document ...

Struggling to retrieve the most recent emitted value from another component in Angular?

Hello everyone, I am currently attempting to retrieve the most recent updated value of a variable from the component app-confirm-bottom-sheet in the app-bene-verification.ts component, but unfortunately, I am unable to achieve this. Below is the code snipp ...

When an input is double-clicked, the message"[object object]" appears on the screen

Here is the structure of the template used for the directive. Our code fetches data from a service which contains all the details of a specific individual. We display only the first name, last name, and either designation or company affiliation from that d ...

The Angular pattern is functioning properly on Plunker, but it is not working when trying

Can someone help me understand this confusing issue? I have the same code in Plunker and locally, but it only works in Plunker. Why is that happening, especially with ng-pattern or HTML5 pattern? In the first image on my local server, I entered "011.11". ...

Guide to developing JavaScript code that moves information from one local website to a different one

Here's the scenario: You input text into a field on website A and click a button. Upon clicking that button, the entered text should appear in website B. I attempted to use localStorage for this functionality, but unfortunately it was not successful. ...

Combining Imported Models in Three.js: A Guide

I'm working on optimizing the draw calls in my THREE.js scene by reducing the amount of rendering required. I have a grid of large 32x32 tiles that are .gtlf models imported from the disk. Each tile has a model with just 8 vertices and a texture. Howe ...

Create a dynamically updating list using React's TypeScript rendering at regular intervals

My goal is to create a game where objects fall from the top of the screen, and when clicked, they disappear and increase the score. However, I am facing an issue where the items are not visible on the screen. I have implemented the use of setInterval to d ...

What is the method for confirming the text within a paragraph element using Nightwatch?

I have a paragraph on a website that needs to be checked for its presence. The HTML code for the element is <p class="subfooter__text" data-v-79ab1348=""> The Tesla is part of the <a href="//www.xyz.com/" target=&quo ...

Searching for the regulations on type-casting in JavaScript

Seeking a definitive set of guidelines regarding automatic typecasting and when it occurs. I am in the process of developing some rules for new developers, an example being: 90 > '100' // comparison as integers '90' > 100 // ...

Is there a specific rule or guideline, like es-lint, that can be used to deactivate the `

How can I implement ES2015 classes without inheritance in our codebase? Is there a specific eslint rule, like "no-extends" or "no-inheritance", that prohibits the use of class A extends B {}? I've tried searching for this rule extensively, but I beli ...

Insert an array inside another array using JavaScript (jQuery)

I've been attempting to use the push() method within a loop to construct a data structure as shown below: var locations2 = [ ['User', position.coords.latitude, position.coords.longitude, 1], ['Bondi Beach', -33.890542, 151 ...

Obtain the input elements and attach a click event to them

I have a form utilizing Bootstrap with three elements. When the button is clicked, another line with three elements is dynamically added. <div class="row align-items-center mb-2 mt-2 ms-1 "> <div class="col-5 ps-1 pe-1"> ...