Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
Angular 2: Length validation not working

I am trying to simply replicate the walkthrough from the Angular website but cannot get length validation to work:

<input 
  style="display:inline-block;min-width:150px" 
  class="form-input" 
  id="phone"
  required 
  minlength="4" 
  maxlength="24"
  type="number" 
  name="phone" 
  [(ngModel)]="client.phone" 
  #phone="ngModel" /> 

  <!-- required works, minlength and maxlength always false -->
  {{ phone.hasError('required') }} {{ phone.hasError('minlength') }} {{ phone.hasError('maxlength') }}

<div *ngIf="phone.errors && (phone.dirty || phone.touched)"
   class="alert alert-danger">
  <div [hidden]="!phone.errors.required">
    Name is required
  </div>
  <div [hidden]="!phone.errors.minlength">
    Name must be at least 4 characters long.
  </div>
  <div [hidden]="!phone.errors.maxlength">
    Name cannot be more than 24 characters long.
  </div>
</div>

I must be missing something simple, but for some reason the required validation changes depending on input, but both minlength and maxlength are always false regardless of how long my input is.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Its because number type doesnt have length property

enter image description here

There is a bug for this https://github.com/angular/angular/issues/15053

You can work around it like:

[ngModel]="client.phone" (ngModelChange)="client.phone = $event + ''"

Plunker Example

Update

Since in 4.2.0-beta.0 (2017-05-04) were introduced min and max validators so you can have a look https://github.com/angular/angular/pull/15813/commits/52b0ec8062381e7285b5f66aa83008edfbf02af3

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda