AngularJS ng-maxlength Directive

Definition and Usage

ng-maxlength directive to add restrictions to input fields and form validators in HTML.

ng-maxlength With the maxlength Properties differ, the latter will prevent users from entering more characters than the limit.

ng-maxlength The directive does not prevent the user from entering more characters than the limit, but if the user does so, the form will be invalid.

Example

If the input value exceeds five characters, an error will be displayed:

<form name="myForm">
<input name="myInput" ng-model="myInput" ng-maxlength="5">
<h1 ng-if="!myForm.myInput.$valid">Value is too long</h1>
</form>

Try It Yourself

Syntax

<input type="text" ng-maxlength="number</input>

Primarily text-based <input> Element supported, but can also be used for other elements that allow user input.

Parameter

Parameter Description
number A number indicating the maximum number of valid characters for an input field.