You can validate in this way as well
In your template(.html file):
<form
[formGroup]="userForm"
#formDirective="ngForm"
(ngSubmit)="onFormSubmit()"
>
<div class="form-control">
<mat-form-field appearance="outline" class="form-field">
<mat-label>Email Address</mat-label>
<input
type="text"
matInput
formControlName="email"
class="form-input-field"
pattern="[a-zA-Z0-9.-_]{1,}@[a-zA-Z.-]{2,}[.]{1}[a-zA-Z]{2,}"
/>
<mat-error *ngIf="userForm.get('email').hasError('required')"
>Email is required</mat-error
>
<mat-error *ngIf="userForm.get('email').hasError('email')"
>Please enter a valid email address
</mat-error>
</mat-form-field>
</div>
</form>
In your component(.ts file):
userForm = new FormGroup({
email: new FormControl('', [Validators.required, Validators.email]),
});