JS:
angular.module('app').directive('numInput',function () { return { require: '^ngModel', scope: true, link: function (scope, el, attrs, ngModelCtrl) { function formatter (value) { let formattedValue = ''; if(!_.isEmpty(value)){ let reg = /^\d+(\.\d+)?$/; let formattedValue = reg.test(value) ? value : ''; el.val(formattedValue); ngModelCtrl.$setViewValue(value); // scope.$apply(); } return formattedValue; } ngModelCtrl.$formatters.push(formatter); el.bind('blur', function () { formatter(el.val()); }); } }; });
html:
<input type="text" ng-model="value" num-input>