Last active
August 29, 2015 14:17
-
-
Save menacestudio/e51d995a6099ab57cf3e to your computer and use it in GitHub Desktop.
An AngularJS decorator directive to only allow numeric values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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