Improve this Doc View Source

ngMinlength adds the minlength `validator` to `ngModel`. It is most often used for text-based `input` controls, but can also be applied to custom text-based controls.

The validator sets the `minlength` error key if the `ngModel.$viewValue` is shorter than the integer obtained by evaluating the Angular expression given in the `ngMinlength` attribute value.

Note: This directive is also added when the plain `minlength` attribute is used, with two differences:

1. `ngMinlength` does not set the `minlength` attribute and therefore HTML5 constraint validation is not available.
2. The `ngMinlength` value must be an expression, while the `minlength` value must be interpolated.

## Directive Info

- This directive executes at priority level 0.

## Usage

- as element:

      <ng-minlength>
      ...
      </ng-minlength>

- as attribute:

      <ANY>
      ...
      </ANY>

## Examples

    <script>
      angular.module('ngMinlengthExample', [])
        .controller('ExampleController', ['$scope', function($scope) {
          $scope.minlength = 3;
        }]);
    </script>
    <div ng-controller="ExampleController">
      <form name="form">
        <label for="minlength">Set a minlength: </label>
        <input type="number" ng-model="minlength" id="minlength" />
        <br>
        <label for="input">This input is restricted by the current minlength: </label>
        <input type="text" ng-model="model" id="input" name="input" ng-minlength="minlength" /><br>
        <hr>
        input valid? = <code>{{form.input.$valid}}</code><br>
        model = <code>{{model}}</code>
      </form>
    </div>

    var model = element(by.binding('model'));
    var input = element(by.id('input'));

    it('should validate the input with the default minlength', function() {
      input.sendKeys('ab');
      expect(model.getText()).not.toContain('ab');

      input.sendKeys('abc');
      expect(model.getText()).toContain('abc');
    });