2017-07-26 1 views
1

Ich benutze eckige 2 formbuilder um ein Formular zu erstellen. Ich möchte nur positive Werte in das Betragsfeld eingeben (minValue ist 0 und maxvalue ist 100) Wie kann ich die min und max Validierung mit angular2 Formbuilder durchführen?winkel 2 formbauer min max validation

Html-Code

<form class="form-horizontal" 
       novalidate 
       (ngSubmit)="splitCharges()" 
       [formGroup]="splitChargeForm" > 
    <input type="number" id="sequenceId" placeholder="Amount" formControlName="amount" > 
</form> 

Winkelkomponente

constructor(private fb: FormBuilder){} 

ngOnInit() { 
     this.splitChargeForm = this.fb.group({ 
      amount: ['', Validators.required], // want to set min=0 and max=100 validations for this field 
      percentage: ['', Validators.required] 
     }); 
    } 

Antwort

2

Dies sollte das tun, was Sie wollen:

amount: ['', [Validators.required, Validators.min(0), Validators.max(100)]], 

Siehe auch https://angular.io/guide/form-validation#component-class-1

+0

@KetanAkbari 'minLength' und' maxLength' ist nicht die Frage, so weit ich es verstehe. –

+0

ok greate, es hilft mir. Vielen Dank –