Home:ALL Converter>angular template driven form validation messages

angular template driven form validation messages

Ask Time:2021-05-21T09:33:45         Author:Kumara

Json Formatter

I am using angular template driven form. Currently I show the error messages inside the HTML page.

    <input 
  type="email" 
  name="email" 
  ngModel 
  #email="ngModel" 
  pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" 
  required>
<br/>

<div *ngIf="email.touched">
  <p *ngIf="email.errors?.required">Email is a required</p>
  <p *ngIf="email.errors?.pattern">This is not a valid Email!!!</p>
</div>

Example Form

I need to move these error messages to my component file. is there any way to do it ?

Author:Kumara,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/67629808/angular-template-driven-form-validation-messages
yy