2

I want to access classes ng-touched and ng-valid to print error message but couldn't figure out how to. Here's my code-

 <form #individual="ngForm">
    <div class="form-group">
      <label for="name">Name:</label>
      <input type="text" class="form-control" id="name" ngModel name="name" pattern="[a-zA-Z ]*" required placeholder="Enter Your Name">
      <label *ngIf="!individual.control.name.valid">INVALID</label>
    </div>
    <button type="submit" class="btn" (click)="onSave(individual)" [disabled]="!individual.valid">SUBMIT</button>
 </form>
1
  • can you share any plunker? Commented Sep 10, 2017 at 6:07

1 Answer 1

1

Add a local variabale on the input which will watch for the model changes, then you can check it's validity:

<input type="text"  #myModel="ngModel"  class="form-control" id="name" ngModel name="name" pattern="[a-zA-Z ]*" required placeholder="Enter Your Name">
<label *ngIf="myModel.invalid">INVALID</label>

or

<label *ngIf="!myModel.valid">INVALID</label>

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

One more thing, Can you help me adding transition to the label? I want it to appear with opacity reveal transition
Yes, sure, but post a separate question please
I have posted a question, do have a look :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.