angular-input-masks
angular-input-masks copied to clipboard
mask for RG (registro geral)
make a mask for RG like "23.344.023-1"
+1, and I can contribute if @assisrafael likes this feature
A pull request is certainly welcome! =)
this would turn this module into a complete set of masks for pt-br webapps. I'll take a look into this this weekend :)
@darlanmendonca @rcrodrigues have you started working on this? If not, I'll work on it :)
I have interest in this feature too, also for RNE. @detinho: do you have started?
I am create the simple directive, based http://pt.stackoverflow.com/questions/22431/express%C3%A3o-regular-para-rg
(function () {
angular
.module("myApp")
.directive('rg', RG);
function RG() {
var directive = {
require: 'ngModel',
link: function (scope, element, attr, ngModelCtrl) {
function parserRG(rg) {
if (rg) {
rg = rg.replace(/\D/g, "");
rg = rg.replace(/(\d{1,2})(\d{3})(\d{3})(\d{1})$/, "$1.$2.$3-$4");
ngModelCtrl.$setViewValue(rg);
ngModelCtrl.$render();
return rg;
}
return undefined;
}
ngModelCtrl.$parsers.push(parserRG);
}
};
return directive;}})();
Good answer @gabrieldevelops but this code have one little problem. Some RG has 'X' char as verify code.
I fix it change these lines:
rg = rg.replace(/\D/g, "");
rg = rg.replace(/(\d{1,2})(\d{3})(\d{3})(\d{1})$/, "$1.$2.$3-$4");
to:
rg = rg.replace(/(?!X)\D/g, "");
rg = rg.replace(/(\d{1,2})(\d{3})(\d{3})([\dX])$/, "$1.$2.$3-$4");
Now the 'X' char can be input in field. The problem now is the 'X' char can be input anywhere on field.
Someone has ideia how fix it now?
(sorry for my Tupiniquim English)
Thanks!
Any news?