angular 2 formbuilder 最小最大验证

angular 2 formbuilder min max validation

我正在使用 angular 2 formbuilder 创建表单。我只想在金额字段中输入正值。(最小值为 0,最大值为 100) 如何使用 angular2 formbuilder 执行最小和最大验证。

Html代码

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

angular分量

 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]
        });
    }

这应该可以满足您的要求:

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

另见 https://angular.io/guide/form-validation#component-class-1