如何使用表单控件在angular 6的文本框中检查负值

时间:2019-02-28 13:28:42

标签: django angular

component.ts

 this.companySettingsForm = this.formBuilder.group({
    'delivary_charge': ['', Validators.compose([Validators.required, ])],  
     });
     this.delivary_charge = this.companySettingsForm.controls['delivary_charge'];
  }
  

在Component.html中,我添加了最小值和最大值,但不起作用

component.html

<input pInputText #value type="number" [formControl]="surcharge" min="1" 
max="9999999999999" class="input-width">

2 个答案:

答案 0 :(得分:0)

您可以在表单控件中使用Validators.minLength()Validators.maxLength()来设置表单控件的最小和最大长度。

要检查负值,可以使用Validators.min()

this.companySettingsForm = this.formBuilder.group({
    'delivary_charge': ['', Validators.compose([Validators.required, Validators.minLength(3), Validators.maxLength(10), Validators.min(0)])],  
 });

答案 1 :(得分:0)

您还可以像这样限制/检查component.ts中的负值

myControl:FormControl = new FormControl();
this.myControl.valueChanges.subscribe(val=>{
  if(val<0){
    // you can check negative values here and do whatever you'd like
    // you can also set the value of your input to null or 0 to prevent negative values 
    // this.myControl.setValue(null);
  }
 });