I am looking for ways to limit the value inside the input to 4 and process the 4 digit value unto my controller.
<input type="number" class="form-control input-lg"
ng-model="search.main" placeholder="enter first 4 digits: 09XX">
{{ search.main | limitTo:4}}
We can write the directive to listen to the
keypress
event. But for some old browsers, this event is not triggered. Thats why i created a directive in such a way that there's no dependency on browser specific events limitation. I created an angular directive to limit the number of text in the input box.Usage:
<input limit-to="3" ng-model="test"/>
would allow only 3 characters in input box.We can use ng-value instead:
In html code:
This will check for the value and if it is greater than 60, it replaces the value with 0.
Can always make a directive for it:
In Angular Js Material we can limit input field by "maxLimit", for example we need limit of input text should b 60 character as:
complete code:
Use this directive if you want to restrict max length for a input field which is present as part of custom directive template. This is the native html5 way of restricting max-lenth. This will also handle the copy paste case also to restrict till the maxlength on pasting.
Angular material has a directive mdMaxlength, if you want to cut off the input at this length, you can use this directive:
Usage: