1

I am currently validating forms using the well prescribed method:

  <form name="userForm">
    <div class="form-group" ng-class="{ 'has-error': userForm.email.$invalid }" >
      <input type="email" class="form-control" name="email" ng-model="user.email" required />
    </div>
  </form>

While this is not the end of the world I wondered if there was a less obtrusive way of doing this. I have already stated the field is required and angular will paint the element with css class based upon it's state. Could I achieve the same ends with CSS rather than using ng-class="{ 'has-error': userForm.email.$invalid }". Many thanks

2
  • Invalid element will have ng-invalid class + other validation classes. Commented Feb 23, 2016 at 11:20
  • Yes, it will also add others like ng-invalid-max depending what has invalidated it. I just wanted to pick up on this in CSS rather than using ng-class everywhere Commented Feb 23, 2016 at 11:27

1 Answer 1

2

You can style the angular classes in your CSS.

.ng-invalid {
    border: 1px solid red;
}

.ng-valid {
    border: 1px solid green;
}

If you want to apply it just to inputs rather than creating a border round the form you can use the following too:

input.ng-invalid {
    border: 1px solid red;
}
Sign up to request clarification or add additional context in comments.

Comments

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.