0

I wish there was TryParse but I guess there isn't in AngularJS. I tried to use angular.isNumber(). What I want my program to do: to replace any not numeric input with number 0. What it actually does: input box ignores anything which is not numeric by not allowing to write it down.

<script>
    var app = angular.module('BindingsApp', []);

    app.controller('InputCtrl', function($scope) {
        $scope.num3 = 0;
        $scope.edit = function () {

            $scope.num1 = parseInt($scope.num1);
            $scope.num2 = parseInt($scope.num2);

            function isNumeric(num) {
                return !isNaN(num);
            }

            if (!angular.isNumber($scope.num1) ) {
                $scope.num1 = 0;
            }

            if (!angular.isNumber($scope.num2) ) {
                $scope.num2 = 0;
            }

            $scope.num3 = $scope.num1 + $scope.num2;
        };
    });
</script>

<input type="text" ng-model="num1" ng-change="edit()" value="isNumeric(num1)" />
<br />
<input type="text" ng-model="num2" ng-change="edit()" value="{{num2}}" />
<br />
<input type="text" ng-model="num3" ng-change="edit()" value="{{num3}}" />
<br />

2 Answers 2

1

Easy way is to replace

<input type="text" ng-model="num1" ng-change="edit()" value="isNumeric(num1)" />
<br />

To

<input type="number" ng-model="num1" ng-change="edit()" />
<br />

type="number" which will allow only numeric values.No need to write any extra code

EDIT: Inorder to replace non-numeric values to 0 and allow numeric values, here is a directive

app.directive('productionQty', function() {
  return {
    require: 'ngModel',
    link: function (scope, element, attr, ngModelCtrl) {
      function fromUser(text) {
        var transformedInput = text.replace(/[^0-9]/g, '0');
        console.log(transformedInput);
        if(transformedInput !== text) {
            ngModelCtrl.$setViewValue(transformedInput);
            ngModelCtrl.$render();
        }
        return transformedInput;
      }
      ngModelCtrl.$parsers.push(fromUser);
    }
  }; 
});

DEMO

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

2 Comments

Thanks, it's a school task and i was directed not to use the type="number" but to solve it with Angular.JS only :-\ (it's also doesn't replace any not numeric input to 0, so it still doesn't answer my needs anyway)
@LifeLess check the demo
0

This should do the trick :

app.controller('InputCtrl', function($scope) {
    $scope.num3 = 0;
    $scope.edit = function () {

        function isNumeric(num) {
            return !isNaN(num);
        }

        if (NaN($scope.num1) ) {
            $scope.num1 = 0;
        }else{
           $scope.num1 = parseInt($scope.num1);
        }

        if (NaN($scope.num2) ) {
            $scope.num2 = 0;
         }
        else{
            $scope.num2 = parseInt($scope.num2);
        }

        $scope.num3 = $scope.num1 + $scope.num2;
    };
});

If you parseInt non numeric character it will set the value to NaN ,so its better not to parse unless you are sure it'a a number. Also for some reason angular.isNumber was considering numeric value of $scope.num1 as not a number.Which is silly.So i replaced it with NaN.Hope this works. I haven't validated with special characters ,so plz check that.

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.