AngularJS – ngMaxlength Directive

The ngMaxlength Directive in AngularJS adds the maxlength validator to the ngModel. This directive is mostly used to control the text-based inputs but can also be applied for controlling the custom text-based controls.

This validator sets the maxlength error key if the value ngModel.$viewValue is longer than the integer value obtained by evaluating the Angular JS expression defined in the ngMaxlength attribute value.

Syntax

..content..

Example − ngMaxlength Directive

Create a file "ngMaxlength.html" in your Angular project directory and copy-paste the following code snippet.


   
      ngMaxlength Directive


   

   
      

         Welcome to Tutorials Point      

     

         AngularJS | ngMaxlength Directive      

     
         
                                   
                       
           
            input valid? = {{form.input.$valid}}
            model = {{model}}          
     
     

Output

To run the above code, just go to your file and run it as a normal HTML file.

Observe the output when the length of the entered text becomes greater than the maxlength, the input becomes invalid.

Updated on: 2021-10-08T12:31:32+05:30

487 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements