Angular

Last Updated: 8/28/2023

Adding Validation

  • Use html5 required attribute
  • Angular use required attribute to add required validation
<input ngModel name="firstName" required #firstName="ngModel">
<div *ngIf="firstName.touched && !firstName.valid" class="alert alert-danger">Firstname is required</div>
  • Use touched property to show error only when the user focus and leaves the control without entering value