Piyush
Piyush

Reputation: 599

How to add class if the validation pass in angular

I am very new to Angular and trying to learn. I am stuck here in form_validation. I am able to add a class when there is an error in that field but I am not getting if suppose there is no error then I need to add another class. This is what I have wrote

<input type="text" 
[class.is-invalid]="name.invalid && (name.dirty || name.touched) && name.errors.minlength" minlength="5" 
required 
name="name" 
#name="ngModel" 
[(ngModel)]="account.name" 
placeholder="Enter your Account name" 
class="form-control">

in the above code, i am able to add is-invalid class but in the else condition, I also want to add is-valid class. Any suggestions ?

Upvotes: 0

Views: 2469

Answers (1)

code-gorilla
code-gorilla

Reputation: 2408

You can have multiple [class.<css-class>] directives on a component, e.g.:

<div
[class.is-invalid]="..."
[class.is-valid]="..."
>
...
</div>

An alternative is using the [ngClass] directive:

<div
[ngClass]="condition ? 'is-invalid' : 'is-valid'"
>
...
</div>

Upvotes: 2

Related Questions