Angular FAQ: Top Questions
37. How do you handle forms validation in Angular?
Angular supports both template-driven and reactive forms validation. Reactive forms use validators from @angular/forms
.
this.form = this.fb.group({
name: ['', [Validators.required, Validators.minLength(3)]]
});
Template example:
<input name="name" ngModel required minlength="3">
- Use custom validators by creating functions implementing
ValidatorFn
.