0

I am trying to toggle css class on input if validation is valid, i can do if is invalid, but i need two classes if is valid and if is not. Is it easy to make it if is not valid, but how to add if is valid, here is my code i have for now:

<form name="userForm" ng-submit="submitForm()" novalidate>
    <input type="email" name="email" placeholder="E-mailadres" class="input-field" ng-model="user.email" required ng-class="{ 'has-error' : userForm.email.$invalid && !userForm.email.$pristine }" />
    <p ng-show="userForm.email.$invalid && !userForm.email.$pristine" class="help-block">Enter a valid email.</p>
    <input type="password" name="password" ng-model="user.password" placeholder="Wachtwoord" class="input-field" required />
    <p ng-show="userForm.password.$invalid && !userForm.password.$pristine" class="help-block">You name is required.</p>
    <button type="submit" class="btn hit-btn" ng-disabled="userForm.$invalid">Log in</button>
</form>

You will notice that i have class has-error but i need also class no-error

3 Answers 3

2

Try this...

.has-error {
  background: red;
}
.no-error {
  background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app>
  <form name="userForm" ng-submit="submitForm()" novalidate>
    <input type="email" name="email" placeholder="E-mailadres" class="input-field" ng-model="user.email" required ng-class="{ 'has-error' : userForm.email.$invalid && !userForm.email.$pristine,'no-error' :!userForm.email.$invalid }" />
    <p ng-show="userForm.email.$invalid && !userForm.email.$pristine" class="help-block">Enter a valid email.</p>
    <input type="password" name="password" ng-model="user.password" placeholder="Wachtwoord" class="input-field" required />
    <p ng-show="userForm.password.$invalid && !userForm.password.$pristine" class="help-block">You name is required.</p>
    <button type="submit" class="btn hit-btn" ng-disabled="userForm.$invalid">Log in</button>
  </form>
</div>

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

Comments

1

If you need another class, just add it with the opposite condition:

<input type="email" name="email" placeholder="E-mailadres"
  class="input-field" ng-model="user.email" required
  ng-class="{
     'has-error' : userForm.email.$invalid && !userForm.email.$pristine, 
     'no-error' : !userForm.email.$invalid || userForm.email.$pristine 
  }"
/>

2 Comments

This not working, now even has-error is not apllied?
@MiomirDancevic sorry I had a spurious ), try again without it.
0
 ng-class="{userForm.email.$invalid && !userForm.email.$pristine?'has-error':'no-error'}"

2 Comments

This not working, now even has-error is not apllied?
Have you define both classes ? scotch.io/tutorials/the-many-ways-to-use-ngclass Ternary operator should work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.