reactive form in angular 11 with validation with Strictly code example

Example 1: angular validators number only in reactive form

new FormControl({value: field.value}, [Validators.required, Validators.pattern(/^-?(0|[1-9]\d*)?$/)])

Example 2: custom validator

import {AbstractControl, ValidatorFn} from '@angular/forms';

export function blue(): ValidatorFn {  
    return (control: AbstractControl): { [key: string]: any } | null =>  
        control.value?.toLowerCase() === 'blue' 
            ? null : {wrongColor: control.value};
}
<>

Example 3: ng2 validations angular using reactiveforms

required error

equalTo error

Tags:

Misc Example