Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reactive forms validators.required is not allowing value 0

inputField: new FormControl('', [Validators.required])

when the user enters '0' in the field, the error required is applied on the control.

Zero should be considered as a value.

like image 974
sravan ponugoti Avatar asked Oct 20 '25 03:10

sravan ponugoti


2 Answers

By default, If you enter anything into formControl element angular form will treat it as truthy value -

inputField: new FormControl(0, [Validators.required])  //valid
inputField: new FormControl('', [Validators.required])  //invalid

But yes, you can add some regex pattern as well to check value match for 0 only, for example -

inputField = new FormControl(0, [Validators.required, Validators.pattern(/^[1-9]*$/)])
like image 161
Pardeep Jain Avatar answered Oct 21 '25 17:10

Pardeep Jain


you're mistaken somewhere.

stackblitz shows 0 meets a required validator: https://stackblitz.com/edit/angular-bmqvtq?file=src/app/app.component.ts

like image 39
bryan60 Avatar answered Oct 21 '25 16:10

bryan60