Skip to content
Advertisement

How to write a validators function that should accept the value between 0 to 30 but not decimal value ?? in Angular

that the if user enter the value between 0 to 30 it should accept 0 ,2, 20 and but should not accept decimal value like 20.1 , 0.1

I have use validators min(0) and validators max(30) but it accept the decimal value Need a validators that should not accept decimal value

If have to use regex pattern, can you give regex pattern code to ??

Advertisement

Answer

Use pattern validator along with min and max. You can also use a single pattern with min max numeric range but then you won’t be able to put different error messages for each. Here we are using 3 validators one for min, one for max and pattern to check only digits:

const control = new FormControl(0, [Validators.min(0), Validators.max(30), Validators.pattern('[0-9]+')]);
Advertisement