how to add validation rule in angular forms code example
Example 1: 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 2: validation minlength angular
Validators.minLength(9)
Example 3: formgroup check if valid
form: FormGroup;
onSubmit(){
//checks if form is valid
if( this.form.valid){
//more code here
}
}