Skip to content

Instantly share code, notes, and snippets.

@menacestudio
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save menacestudio/e51d995a6099ab57cf3e to your computer and use it in GitHub Desktop.
Save menacestudio/e51d995a6099ab57cf3e to your computer and use it in GitHub Desktop.
An AngularJS decorator directive to only allow numeric values.
numericOnly.$inject = ['$log'];
function numericOnly(
$log: ng.ILogService): ng.IDirective {
return <ng.IDirective>{
restrict: 'A',
scope: true,
link: link
};
function link($scope, $element, $attrs) {
$element.bind('keydown', function (event) {
var charCode = (event.which) ? event.which : event.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
});
}
}
angular.module('app').directive('numericOnly', numericOnly);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment