0

I have a problem with validating numbers. Whenever I input a zero on the number field, it's just still accepts the submission.

<tbody>

 <tr ng-repeat="item in requisitionItems | orderBy:predicate:reverse | filter: searchText">
 <td>
 <input type="checkbox" ng-click="cbChecked()" ng-disabled="item.DeliveredQuantity != 0" ng-model="item.isItemSelected" />
 </td>
 @*<td ng-disabled="item.DeliveredQuantity != 0" ng-class="{red : item.DeliveredQuantity == 0}"><input type="checkbox" ng-disabled="item.DeliveredQuantity != 0" ng-click="cbChecked()" ng-model="item.isItemSelected" /></td>*@
  <td ng-disabled="item.BalanceQuantity != 0" ng-class="{red : item.DeliveredQuantity == 0}"><span ng-bind="item.InventoryItemID"></span></td>
  <td ng-disabled="item.BalanceQuantity != 0" ng-class="{red : item.DeliveredQuantity == 0}"><span ng-bind="item.ItemName"></span></td>
  <td ng-disabled="item.BalanceQuantity != 0" ng-class="{red : item.DeliveredQuantity == 0}"><span ng-bind="item.Quantity"></span></td>
  <td ng-disabled="item.BalanceQuantity != 0" ng-class="{red : item.DeliveredQuantity == 0}"><span ng-bind="item.UnitOfMeasurement"></span></td>
  <td ng-show="deliveryType=='PartialDelivery'">
  <input type="number" ng-model="item.DeliveredQuantity"  class="form-control" min="1" max="100" onkeypress="return isNumberKey(event)" name="Quantity">
  </td>

Thanks!

1 Answer 1

1

You can show an error

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
});

app.directive('numbersOnly', function() {
  return {
    require: 'ngModel',
    link: function(scope, element, attrs, modelCtrl) {
      modelCtrl.$parsers.push(function(inputValue) {


        if (parseInt(inputValue) <= 100 && parseInt(inputValue) > 0) {
          modelCtrl.$setValidity('numbersOnly', true);
          return inputValue;
        } else {
          modelCtrl.$setValidity('numbersOnly', false);
          return modelCtrl.$modelValue;
        }

      });
    }
  };
});

function MyCtrl($scope) {
  $scope.number = ''
}

Working example http://plnkr.co/edit/zhuPADr7Y7lUaQZgzIOk?p=preview

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.