Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you limit the value from input using AngularJS?

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}} 
like image 204
bluestella Avatar asked Jul 11 '14 15:07

bluestella


2 Answers

Can always make a directive for it:

app.directive("limitTo", [function() {     return {         restrict: "A",         link: function(scope, elem, attrs) {             var limit = parseInt(attrs.limitTo);             angular.element(elem).on("keypress", function(e) {                 if (this.value.length == limit) e.preventDefault();             });         }     } }]);  <input limit-to="4" type="number" class="form-control input-lg" ng-model="search.main" placeholder="enter first 4 digits: 09XX"> 
like image 80
tymeJV Avatar answered Sep 18 '22 20:09

tymeJV


You can always use ng-minlength, ng-maxlength for string length and min, max for number limits. Try this

<input type="number"         name="input"        class="form-control input-lg"        ng-model="search.main"         placeholder="enter first 4 digits: 09XX"        ng-minlength="1"         ng-maxlength="4"         min="1"         max="9999"> 

DEMO FIDDLE

like image 40
Raghavendra Avatar answered Sep 19 '22 20:09

Raghavendra