Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
How to validate input field for 11bits number only in Angular?

I have an Input field in my angular template-driven form. I want to configure that fields so that it can only take an 11bits number like 01001111100

<input
    class="form-control"
    id="bitsValue"  
    [placeholder]="field.templateOptions.placeholder"
    pattern="[0-1]{1}"
    [(ngModel)]="ABC"
>

How can I do that?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you were to use reactive forms, create you form for example:

this.myForm = this._sb.fb.group(
{
    bitsValue: this._sb.fb.control('', [Validators.required, YourValidatorClass.elevenBitsOnly])        
};
    
    
export class YourValidatorClass {
        
static elevenBitsOnly(control: AbstractControl): ValidationErrors | null {
   const value = control.value;
   //... validate your value meets your 11 bit requirement
         return { bitValueReq: 'You must provide value as 11 bits number' };
   //... if it doesn't meet the requirement return null
         return null;
   }
}

Then in your html template:

<div [formGroup]="myForm">
 <mat-form-field>       
        <input matInput type="text" formControlName="bitsValue"  />
        <mat-error *ngIf="myForm.hasError('bitValueReq'))"> {{ getErrorMessage(myForm.get('bitsValue')) }}</mat-error>
    </mat-form-field>
</div>

....component.ts file:

getErrorMessage(control: AbstractControl): string{
  const error = control.getError('bitValueReq');
    if(error){  ​
  ​  return error['bitValueReq'];
    }
  }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda