Home > Software engineering >  How not to display an error message if the field was not touched?
How not to display an error message if the field was not touched?

Time:12-16

How to display the "Email is required" message if a field has been touched

<div >
    <label >Email</label>
    <input  type="text" formControlName="login" required>
    <span  *ngIf="form.controls.login.errors?.required">
        Email is required
    </span>
</div>

CodePudding user response:

You can use form.controls.login.touched

<div >
        <label >Email</label>
        <input  type="text" formControlName="login" required>
        <span  *ngIf="form.controls.login.touched && form.controls.login.errors?.required">
            Email is required
        </span>
    </div>

CodePudding user response:

With touched.

<span  *ngIf="form.controls.login.touched && form.controls.login.errors?.required">
    Email is required
</span>

Sample Demo on StackBlitz

References

Validating input in template-driven forms

  • Related