AngularJS | ng-maxlength Directive
The ng-maxlength Directive in AngularJS is used to set the maximum length for an input field i.e it adds the restriction for an input field. It is different from maxlength attribute in HTML because the former prevent users from exceeding the limit whereas the later doesn’t do that. It will make the form invalid if the input limit exceeds.
Syntax:
<element ng-maxlength="expression"> Contents... </element>
Where expression is a number denoting the maximum limit upto which the input is valid.
Example: This example uses ng-maxlength Directive to check the maximum length of string.
<!DOCTYPE html> <html> <head> <title>ng-maxlength Directive</title> <script src= </script> </head> <body style="text-align: center"> <h1 style="color:green">GeeksforGeeks</h1> <h2>ng-maxlength Directive</h2> <div ng-app="app" ng-controller="geek"> <form name="maxlen" novalidate ng-submit= "maxlen.$valid &&maxlength;()"> <pre>Atmax 5 characters required</pre> Input: <input type="text" name="code" ng-model="txt" ng-maxlength="5" required /> <br><br> <button type="submit">Click it</button> <br><br> <span>{{msg}}</span> </form> </div> <script> var app = angular.module('app', []); app.controller('geek', function ($scope) { $scope.maxlength = function () { $scope.msg = "Input is valid"; }; }); </script> </body> </html> |
Output:
Before clicking the button:

After clicking the button:

Recommended Posts:
- AngularJS | ng-app Directive
- AngularJS | ng-if Directive
- AngularJS | ng-jq Directive
- AngularJS | ng-src Directive
- AngularJS | ng-cut Directive
- AngularJS | ng-value Directive
- AngularJS | ng-csp Directive
- AngularJS | Directive
- AngularJS | ng-selected Directive
- AngularJS | ng-required Directive
- AngularJS | ng-paste Directive
- AngularJS | ng-srcset Directive
- AngularJS | ng-readonly Directive
- AngularJS | ng-blur Directive
- AngularJS | ng-bind Directive
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


