Validate input size with AngularS

3

I have a field in a form that I need to validate with angle. The field must be up to 11 characters long. If you have less than 5 you should get an error message requiring the user to be numbered correctly.

Angle Controller

angular.module('xxx') .controller('yyyController', function ($scope) {
    var vm = $scope;      
    vm.validarCPF = function(){

      if( condicao < 11){
       //mensagem de erro
      }

    };

HTML Code

<input type="text" name="cpf" id="cpf" ng-model="cpfusuario">
    
asked by anonymous 10.02.2017 / 19:17

1 answer

7

You can use the ng-minlength and ng-maxlength directives in the HTML as below:

<div ng-controller="ExampleController">
  <form name="meuForm">
    <label>
       User name:
       <input type="text" name="cpf" id="cpf" ng-model="cpfusuario" ng-minlength="5" ng-maxlength="11" maxlength="11">
    </label>
    <div role="alert">
      <span class="error" ng-show="meuForm.cpf.$error.minlength">
        Tamanho mínimo de 5!</span>
      <span class="error" ng-show="meuForm.cpf.$error.maxlength">
        Tamanho máximo de 11!</span>
    </div>
  </form>
</div>

See more in the AngularJS documentation: link

    
10.02.2017 / 19:27