Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate email address from a mat input?(Angular Material)

Tags:

angular

How can I validate an email address to have an @ sign and a dot without pressing on a button, without a form. I would appreciate any help.

<mat-form-field class="example-full-width">
  <input matInput placeholder="Favorite food" value="Sushi">
</mat-form-field>
like image 794
ryy77 Avatar asked Dec 03 '22 10:12

ryy77


2 Answers

You could use pattern and define a regex to validate email address,

<input matInput placeholder="Favorite food" [(ngModel)]="enterEmail" name="myEmail" pattern="[a-zA-Z0-9.-_]{1,}@[a-zA-Z.-]{2,}[.]{1}[a-zA-Z]{2,}" required>
like image 61
Sajeetharan Avatar answered Dec 15 '22 00:12

Sajeetharan


I'm using Angular 8, and it has directive EmailValidator.

Use with ngModel like this:

<input type="email" name="email" ngModel email>
<input type="email" name="email" ngModel email="true">
<input type="email" name="email" ngModel [email]="true">
like image 23
Robin Huy Avatar answered Dec 15 '22 00:12

Robin Huy