title: Allowing user to type only positive numbers in input box using angularjs
body: <p>I want to allow the user to enter only positive numbers in the textbox</p>
  
  <p>My code is as follows:</p>
  
  <p>script.js file contents:</p>
  
  <pre><code>angular.module("myfapp", []).controller("HelloController", function($scope) {
  $scope.helloTo = {};
  $scope.helloTo.title = "AngularJS";
  });
  
  angular.module('myApp', []).controller('MainCtrl', function($scope) {
  app.directive('validNumber', function() {
    return {
  require: '?ngModel',
  link: function(scope, element, attrs, ngModelCtrl) {
    if (!ngModelCtrl) {
      return;
    }
  
    ngModelCtrl.$parsers.push(function(val) {
      var clean = val.replace(/[^0-9]+/g, '');
      if (val !== clean) {
        ngModelCtrl.$setViewValue(clean);
        ngModelCtrl.$render();
      }
      return clean;
    });
  
    element.bind('keypress', function(event) {
      if (event.keyCode === 32) {
        event.preventDefault();
      }
    });
  }
  };
  });
  });
  </code></pre>
  
  <p>angular.html contents as follows:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;script src="angular.min.js"&gt;&lt;/script&gt;
  &lt;script src="script.js"&gt;&lt;/script&gt;
  
  &lt;style&gt;
  .entry {
  width: 300px;
  margin: 10px auto;
  text-align: center;
  }
  &lt;/style&gt;
  
  &lt;/head&gt;
  &lt;body ng-app="myfapp"&gt;
  &lt;div ng-controller="HelloController" &gt;
  &lt;h2 class="entry"&gt;Welcome {{ helloTo.title }} to the world of        Tutorialspoint!&lt;/h2&gt;
  &lt;/div&gt;
  
  &lt;section ng-app="myApp" ng-controller="MainCtrl"&gt;
   &lt;h4 class="entry"&gt;AngularJS Numeric Value Widget&lt;/h4&gt;
  &lt;div class="well entry"&gt;
  &lt;label&gt;Employee Age
    &lt;input type="text" ng-model="employee.age"  placeholder="Enter an age"    valid-number/&gt;
     &lt;/label&gt;
  &lt;/div&gt;
  &lt;/section&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Why does it not work?
  Can anyone run it and check please!</p>