ngcpf
Version:
A module to validate and format cpf
68 lines • 2.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var forms_1 = require("@angular/forms");
var CpfMaskDirective = (function () {
function CpfMaskDirective() {
this.cpfMask = '999.999.999-99';
}
CpfMaskDirective.prototype.writeValue = function (value) { };
CpfMaskDirective.prototype.registerOnChange = function (fn) {
this.onChange = fn;
};
CpfMaskDirective.prototype.registerOnTouched = function (fn) {
this.onTouched = fn;
};
CpfMaskDirective.prototype.onKeyup = function ($event) {
var value = $event.target.value.replace(/\D/g, '');
var pad = this.cpfMask.replace(/\D/g, '').replace(/9/g, '_');
var valueMask = value + pad.substring(0, pad.length - value.length);
if ($event.keyCode === 8) {
this.onChange(value);
return;
}
if (value.length <= pad.length) {
this.onChange(value);
}
var valorMaskPos = 0;
value = '';
for (var i = 0; i < this.cpfMask.length; i++) {
if (isNaN(parseInt(this.cpfMask.charAt(i)))) {
value += this.cpfMask.charAt(i);
}
else {
value += valueMask[valorMaskPos++];
}
}
if (value.indexOf('_') > -1) {
value = value.substr(0, value.indexOf('_'));
}
$event.target.value = value;
};
CpfMaskDirective.prototype.onBlur = function ($event) {
if ($event.target.value.length === this.cpfMask.length) {
return;
}
this.onChange('');
$event.target.value = '';
};
return CpfMaskDirective;
}());
CpfMaskDirective.decorators = [
{ type: core_1.Directive, args: [{
selector: '[cpfMask]',
providers: [{
provide: forms_1.NG_VALUE_ACCESSOR,
useExisting: CpfMaskDirective,
multi: true
}]
},] },
];
/** @nocollapse */
CpfMaskDirective.ctorParameters = function () { return []; };
CpfMaskDirective.propDecorators = {
'onKeyup': [{ type: core_1.HostListener, args: ['keyup', ['$event'],] },],
'onBlur': [{ type: core_1.HostListener, args: ['blur', ['$event'],] },],
};
exports.CpfMaskDirective = CpfMaskDirective;
//# sourceMappingURL=cpf-mask.directive.js.map