0

I am looking at an angular directive for numeric input only:

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

It is letting the space through as a valid character, which interferes with the maxlength property on the INPUT element. How to modify it to prevent the space?

Community
  • 1
  • 1
Tim
  • 8,669
  • 31
  • 105
  • 183
  • It actually shouldn't do this. For me this regex looks rock solid. What error is thrown exactly? – David Losert Oct 07 '14 at 19:43
  • No error. If you wouldn't mind, could you visit his Plunkr link and see if the behavior there reveals something? Try typing `12{space}` and see if you can then enter a third digit. – Tim Oct 07 '14 at 19:46
  • Can you not just use type="num" to use the built in angular way of allowing only numeric? – Scott Oct 07 '14 at 19:52
  • Type="num" allows decimals and I'm looking for integer-only. We want the input to ignore everything but 0-9 – Tim Oct 07 '14 at 19:58

0 Answers0