我使用angular 2 formbuilder来创建表单。我只想在金额字段中输入正值。(minValue为0,maxvalue为100) 如何使用angular2 formbuilder执行最小和最大验证。
Html代码
<form class="form-horizontal"
novalidate
(ngSubmit)="splitCharges()"
[formGroup]="splitChargeForm" >
<input type="number" id="sequenceId" placeholder="Amount" formControlName="amount" >
</form>
角度组件
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]
});
}
答案 0 :(得分:3)
这应该做你想要的:
amount: ['', [Validators.required, Validators.min(0), Validators.max(100)]],
另见https://angular.io/guide/form-validation#component-class-1