0
component.ts

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

In Component.html i added min and max values, but it won't work

component.html

<input pInputText #value type="number" [formControl]="surcharge" min="1" 
max="9999999999999" class="input-width">
AStopher
  • 4,207
  • 11
  • 50
  • 75
Saisiva A
  • 595
  • 6
  • 24

2 Answers2

0

You can set the min and max length of your form control using Validators.minLength() and Validators.maxLength() in your form control.

To check negative values you can use Validators.min()

this.companySettingsForm = this.formBuilder.group({
    'delivary_charge': ['', Validators.compose([Validators.required, Validators.minLength(3), Validators.maxLength(10), Validators.min(0)])],  
 });
Nithya Rajan
  • 4,722
  • 19
  • 30
0

You could also restrict/check negative values in your component.ts like this

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);
  }
 });
Bogdan B
  • 846
  • 9
  • 23
  • 1
    Angular can perform the work for you, Why not take advantage of it? – Eliseo Feb 28 '19 at 14:10
  • As far as i know, Validators.min(0) will not validate the form if the value is lesse than 0. What i'm suggesting is to not allow the users to type a value lesser than 0 and try to submit it. – Bogdan B Feb 28 '19 at 14:33