I am doing a field validation in Angular component. There is a one field like it should allow to enter either 6 or 8 length of any characters. Usually minLength(6), maxLength(8) will work if it is 6 to 8 in between but length 7 should not allow here. I can do custom validation, but out of curiosity I’m looking is there any way to handle this kind of scenario. Thanks in advance.
'myField': ['', [Validators.minLength(6), Validators.maxLength(8)]]
>Solution :
The only way without a custom validator I can think of is Validator.pattern , which takes a regular expression. Something like that:
myControl: FormControl = new FormControl(undefined, [
Validators.pattern('^.{6}$|^.{8}$'),
]);