How can you limit the value from input using AngularJS?

99,768

Solution 1

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">

Solution 2

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

Solution 3

No need to use an AngularJS directive.

Just use the good old standard html attribute maxlength.

<input type="text" maxlength="30" />

Solution 4

I used the accepted answer as a launching point, but this is what I came up with.

angular.module('beastTimerApp')
  .directive('awLimitLength', function () {
  return {
    restrict: "A",
    require: 'ngModel',
    link: function (scope, element, attrs, ngModel) {
      attrs.$set("ngTrim", "false");
      var limitLength = parseInt(attrs.awLimitLength, 10);// console.log(attrs);
      scope.$watch(attrs.ngModel, function(newValue) {
        if(ngModel.$viewValue.length>limitLength){
          ngModel.$setViewValue( ngModel.$viewValue.substring(0, limitLength ) );
          ngModel.$render();
        }
      });
    }
  };
});

usage

<input name="name" type="text"  ng-model="nameVar" aw-limit-length="10"/>

The key is to use $setViewValue() and $render() to set and display the changes respectively. This will make sure $viewValue and $modelValue are correct and displayed properly. You also want to set ngTrim to false to prevent the user adding whitespaces beyond the limit. This answer is an amalgamation of @tymeJV's answer and this blog post... https://justforchangesake.wordpress.com/2015/01/10/useful-angularjs-directives/

Solution 5

Will do it allowing backspaces and deletes.

app.directive("limitTo", [function() {
    return {
    restrict: "A",
        link: function(scope, elem, attrs) {
            var limit = parseInt(attrs.limitTo);
            angular.element(elem).on("keydown", function() {
                if(event.keyCode > 47 && event.keyCode < 127) {
                    if (this.value.length == limit) 
                        return false;
                }   
            }); 
        }
    }
}]);
Share:
99,768
bluestella
Author by

bluestella

Updated on July 12, 2020

Comments

  • bluestella
    bluestella almost 4 years

    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}}