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

379
Visualizações
Angular Reactive Form Pattern not working

I'm using Reactive Form on Angular to be able to use both numbers and commas. I made the input type text so that dots and commas appear on the keyboard in the mobile view. I want the entered input to accept only numbers, but the pattern I entered did not help. I can still enter characters after doing this, but I don't want that to happen.

textInput: new FormControl('', 
[
  Validators.required,
  Validators.pattern('[0-9]{7}')
]),

<input autoComplete="off" autoCorrect="off"
    type="text"
    lang="en"
    [formControl]="this.config.textInput"
    placeholder="0.0"
    minLength={1}
    maxLength={79}
    spellCheck="false"
    (ngModelChange)="amountChanged($event)"
    enterkeyhint="next"
    step="1"
    class="w-100"
>

How can I get the type to be text and only accept numbers?

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

0

You can use input type: number for example :

<input autoComplete="off" autoCorrect="off"
type="number"
lang="en"
[formControl]="this.config.textInput"
placeholder="0.0"
minLength={1}
maxLength={79}
spellCheck="false"
(ngModelChange)="amountChanged($event)"
enterkeyhint="next"
step="1"
class="w-100">
over 4 years ago · Santiago Trujillo Relatório

0

You can update the below pattern for validating your input field.

  textInput = new FormControl("", [
    Validators.required,
    Validators.pattern("[0-9]*")
  ]);

[0-9]* this may work for you.

I have added demo-link and screenshot below of my example:

Demo Link

enter image description here enter image description here enter image description here

Please check and share your feedback.

Thanks.

over 4 years ago · Santiago Trujillo Relatório

0

You can crate directive for it like

@Directive({
  selector: '[only-number]',
})
export class OnlyNumberDirective implements OnInit {
  constructor() {}

  ngOnInit() {}

  @HostListener('input', ['$event'])
  inputEvent(event: KeyboardEvent) {
    const input = event.target as HTMLInputElement;
    input.value = input.value.replace(/[^\d]+/g, '');
  }
}

declare in module

 declarations: [AppComponent, OnlyNumberDirective],

To use

<input type="text" only-number />

Demo

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