0

Trying to add specific class to input if validation fails. below is a simple testing code.

controller:

vm.emsg = '';
if ((parseFloat(vm.sum1) + parseFloat(vm.sum2)) < 100) {
    vm.emsg = 'Hey, sum should be 100.';

   /*something to add class to input*/

    return false;
}

template:

<form method="POST" enctype="multipart/form-data" ng-submit="vm.save($event)">
    <input type="text" ng-model="vm.sum1">
    <input type="text" ng-model="vm.sum2">
    <div class="error-message" ng-if="vm.emsg">
        {[vm.emsg]}
    </div>
</form>
2
  • Do you have a <form> element around the inputs? Usually the form drives the validation. Like if you had <form name="inputForm"> you could check your inputs as a property off of them with ng-class Commented Apr 4, 2017 at 20:57
  • yes I have form around inputs, let me edit Commented Apr 4, 2017 at 21:11

1 Answer 1

1

An option is to use the ngClass directive evaluating your expression, you can also evaluate your expression to display an appropriate message like so:

// app.js
(function() {

  'use strict';

  angular.module('app', []);

})();

// main.controller.js
(function() {

  'use strict';

  angular.module('app').controller('MainController', MainController);

  MainController.$inject = [];

  function MainController() {

    var vm = this;

  }

})();
.error-message {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>

<div ng-app="app" ng-controller="MainController as vm">
  <input type="number" ng-model="vm.sum1">
  <input type="number" ng-model="vm.sum2">
  <div ng-class="{'error-message': vm.sum1 + vm.sum2 !== 100}">
    {{vm.sum1 + vm.sum2 !== 100 ? 'Hey, sum should be 100.' : ''}}
  </div>
  {{vm.sum1 + vm.sum2}}
</div>

Or you can use the ngIf directive to display your message:

// app.js
(function() {

  'use strict';

  angular.module('app', []);

})();

// main.controller.js
(function() {

  'use strict';

  angular.module('app').controller('MainController', MainController);

  MainController.$inject = [];

  function MainController() {

    var vm = this;

    vm.sumsInvalid = sumsInvalid;

    function sumsInvalid() {

      // simply return true to display the error message via the ngIf directive
      return parseFloat(vm.sum1) + parseFloat(vm.sum2) !== 100;

    }

  }

})();
.error-message {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>

<div ng-app="app" ng-controller="MainController as vm">

  <input type="number" ng-model="vm.sum1">

  <input type="number" ng-model="vm.sum2">

  <div class="error-message" ng-if="vm.sumsInvalid()">
    Hey, sum should be 100.
  </div>

  <p>{{vm.sum1 + vm.sum2}}</p>

</div>

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

2 Comments

what I am trying to do is to add class to inputs, alongside message, though would prefer to handle it within controller if possible
if you look at my code I use ng-if, but in my example without function

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.