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

840
Visualizações
How to add Validation pattern not to allow only space in input Angular2?

I used formBuilder in Angular2 and want to add validation pattern for not to allow "only spaces" in input.

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

0

Use the following:

Validators.pattern(/^\S*$/)

DEMO

about 4 years ago · Santiago Trujillo Relatório

0

space Not allowed

let nospacePattern = [a-zA-Z0-9]

Update

As per requirement in comment section.

need pattern not to allow only spaces. (space in between words are allowed).but when user enter spaces in input and try to save it then it should not allow to save

Validators.pattern(".*\\S.*[a-zA-z0-9 ]");

Update 2

Better and Cleaner way to use custom validation pattern like below -

controlName: ['', [Validators.required, this.noWhitespaceValidator]],

....
....
noWhitespaceValidator(control: FormControl) {
    const isWhitespace = (control && control.value && control.value.toString() || '').trim().length === 0;
    const isValid = !isWhitespace;
    return isValid ? null : { 'whitespace': true };
  }
about 4 years ago · Santiago Trujillo Relatório

0

try this, it will return false while sapce key press :

@Component({
  selector: 'my-app',
  template: `
    <div>
       <input class="form-control" type="number" formControlName="pinCode" placeholder="Pin Code"
             (keydown.space)="$event.preventDefault()">
    </div>
  `,
  providers: [myService]
})

visit for more Events :

https://developer.mozilla.org/en-US/docs/Web/Events

about 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