Skip to content

Instantly share code, notes, and snippets.

@scizers
Last active August 29, 2015 14:02
Show Gist options
  • Save scizers/484716ac72f3d1496b45 to your computer and use it in GitHub Desktop.
Save scizers/484716ac72f3d1496b45 to your computer and use it in GitHub Desktop.
Angular Directive to Allow Natural Numbres Only in the input tag
app.directive('decimalOnly', function () {
return {
require: 'ngModel',
link: function (scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
if (inputValue == undefined) return ''
console.log(inputValue)
var transformedInput = inputValue.replace(/[^0-9\\.]/g, '');
if (transformedInput != inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return parseFloat(transformedInput);
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment