Validate form with all fields in AngularJs

What is the best way to validate all the fields in AngularJS.

I have a form with 7 fields, and all are mandatory, and I want to show an error message or change in a border if any of the fields is not valid.

I use below approach. Please tell me is it correct way. What if tomorrow I have 100 fields.

    $scope.emailError = true;
else {
    $scope.emailError = false;
    $scope.passwordError = true;
else {
    $scope.passwordError = false;


You can handle this in the template, see more information in the doc:

From the docs

<form name="myForm" class="css-form" novalidate>
    <input type="email" name="input" ng-model="email.text" required>

    <span class="error" ng-show="myForm.input.$error.required">
    <span class="error" ng-show="myForm.input.$">
        Not valid email!

  <!-- If you want to disable the button until all fields are filled in correctly.-->
  <button class="btn btn-default" ng-disabled="myForm.$invalid">Login</button>

As Alex said it’s also a good idea to look into ngMessages, you can find helpful information here:

Leave a Reply

Your email address will not be published. Required fields are marked *