Skip to content

Instantly share code, notes, and snippets.

@olaferlandsen
Created December 5, 2016 18:00
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 olaferlandsen/c07d74d8664f163cf08d34c702b2d6bc to your computer and use it in GitHub Desktop.
Save olaferlandsen/c07d74d8664f163cf08d34c702b2d6bc to your computer and use it in GitHub Desktop.
A simple RUT(chile) mask for Angular.
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