Skip to content

Instantly share code, notes, and snippets.

@Kyle-Muir
Created September 7, 2015 20:01
Show Gist options
  • Save Kyle-Muir/065c8377799616d77ec6 to your computer and use it in GitHub Desktop.
Save Kyle-Muir/065c8377799616d77ec6 to your computer and use it in GitHub Desktop.
Creates an angular 1.x directive to remove non-numerical input from input fields
directives.directive('intergenNumbersOnly', () => {
'use strict';
return {
require: 'ngModel',
link: (scope, element, attrs, modelCtrl) => {
modelCtrl.$parsers.push(inputValue => {
if (inputValue === undefined) {
return '';
}
var transformedInput = inputValue.replace(/[^0-9]/g, '');
if (transformedInput !== inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment