Created
December 5, 2016 18:00
-
-
Save olaferlandsen/c07d74d8664f163cf08d34c702b2d6bc to your computer and use it in GitHub Desktop.
A simple RUT(chile) mask for Angular.
This file contains 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
angular.module('olaferlandsen', []) | |
.directive('ngRut', function() { | |
return function (scope, element, attrs) { | |
element.bind('keypress', function (event) { | |
var value = element.val().replace(/[^0-9k]/ig, ''); | |
if (value.length == 9) event.preventDefault() | |
if ( (event.which >= 48 && event.which <= 57) || event.which == 75) { | |
// reemplazamos todo lo que no sea parte del rut | |
// establecemos el guion medio(-) | |
if (value.length == 7 || value.length == 8) element.val(value + '-') | |
} | |
else { | |
event.preventDefault() | |
} | |
}); | |
}; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment