I have an AngularJS web app that has a form containing a textarea as follows:
<textarea required ng-maxlength="1000" ng-model="foo"></textarea>
The validation logic simply says that the user must enter a value up to a maximum of 1000 characters. Suppose further that I decide that I want the app to show the user how many characters remain before the limit is reached. No problem: I simply write this:
<div ng-if="foo.length <= 1000">
{{ 1000 - foo.length }} characters remaining
</div>
The above works perfectly. Now, suppose that I want to display a message indicating that the user has typed too many characters, so I write the following:
<div ng-if="foo.length > 1000">
{{ foo.length - 1000 }} characters too many
</div>
The above code does not work and it appears that such is the case because the input value is not accepted by Angular, so foo is left undefined, and I can't tell the user how many characters must be removed for the input to be valid.
Therefore, I would like to know if there is a way to get the value of an invalid input and tell the user a meaningful reason that it is invalid.
someForm.lotsaText.$error.maxlength
inside ofng-if
so you don't have to check the length. – Rackety