Commit b0754146c8f6be0ccbaca954cd5b7dd305ebad83
1 parent
c0cb35795d
Exists in
develop
Add
Nueva directiva
Showing
1 changed file
with
25 additions
and
0 deletions
 
Show diff stats
src/js/onlyNumbers-directive.js
| ... | ... | @@ -0,0 +1,25 @@ | 
| 1 | +angular.module('focaDirectivas') | |
| 2 | + .directive('onlyNumbers', function () { | |
| 3 | + return { | |
| 4 | + restrict: 'A', | |
| 5 | + link: function (scope, elm, attrs, ctrl) { | |
| 6 | + elm.on('keydown', function (event) { | |
| 7 | + if (event.shiftKey) { event.preventDefault(); return false; } | |
| 8 | + //console.log(event.which); | |
| 9 | + if ([8, 13, 27, 37, 38, 39, 40].indexOf(event.which) > -1) { | |
| 10 | + // backspace, enter, escape, arrows | |
| 11 | + return true; | |
| 12 | + } else if ( | |
| 13 | + event.which >= 48 && event.which <= 57 || | |
| 14 | + event.which >= 96 && event.which <= 105 | |
| 15 | + ) { | |
| 16 | + // numbers 0 to 9 or numpad number | |
| 17 | + return true; | |
| 18 | + } else { | |
| 19 | + event.preventDefault(); | |
| 20 | + return false; | |
| 21 | + } | |
| 22 | + }); | |
| 23 | + } | |
| 24 | + } | |
| 25 | + }); |