Text field formatting for PIS / PASEP

3

It's not really a question, it's an aid. I've been searching PIS / PASEP mask and did not find it so it was the way to implement it.

I'll put the answer below.

Hugs.

    
asked by anonymous 08.03.2017 / 20:04

1 answer

3

The following functions are required below.

// Main function

function mascara(o, f) {
   v_obj = o
   v_fun = f
   setTimeout("execmascara()", 1)
}

// Function of running any mask.

function execmascara() {
   v_obj.value = v_fun(v_obj.value)
}

// PisPasep Formatting Function // return '000.00000.00.0'

function pispasep(v) {
    v = v.replace(/\D/g, "")                                      //Remove tudo o que não é dígito
    v = v.replace(/^(\d{3})(\d)/, "$1.$2")                        //Coloca ponto entre o terceiro e o quarto dígitos
    v = v.replace(/^(\d{3})\.(\d{5})(\d)/, "$1.$2.$3")            //Coloca ponto entre o quinto e o sexto dígitos
    v = v.replace(/(\d{3})\.(\d{5})\.(\d{2})(\d)/, "$1.$2.$3.$4") //Coloca ponto entre o décimo e o décimo primeiro dígitos
    return v
}

// PisPasep number validation function

function validarPIS (pis) {

var multiplicadorBase = "3298765432";
var total = 0;
var resto = 0;
var multiplicando = 0;
var multiplicador = 0;
var digito = 99;

// Retira a mascara
var numeroPIS = pis.replace(/[^\d]+/g, '');
if (numeroPIS.length !== 11 ||
    numeroPIS === "00000000000" ||
    numeroPIS === "11111111111" ||
    numeroPIS === "22222222222" ||
    numeroPIS === "33333333333" ||
    numeroPIS === "44444444444" ||
    numeroPIS === "55555555555" ||
    numeroPIS === "66666666666" ||
    numeroPIS === "77777777777" ||
    numeroPIS === "88888888888" ||
    numeroPIS === "99999999999") {
    return false;
} else {
    for (var i = 0; i < 10; i++) {
        multiplicando = parseInt(numeroPIS.substring(i, i + 1));
        multiplicador = parseInt(multiplicadorBase.substring(i, i + 1));
        total += multiplicando * multiplicador;
    }
    resto = 11 - total % 11;
    resto = resto === 10 || resto === 11 ? 0 : resto;
    digito = parseInt("" + numeroPIS.charAt(10));
    return resto === digito;
}

} Example: Add an input and place the call to the masquerade method.

Pis/Pasep: "<"input type="text" id="pis" onkeyup="mascara(this, pispasep);" maxlength="14" onblur="if(validarPIS(this.value)){}else{alert('Número do PisPasep Inválido!');}"/>

Important: Do not forget the maxlength="14"

Validation: In the example I put the validation is done in the onblur of the input showing the message Invalid PisPasep Number!

Hugs!

César Rabelo!

    
08.03.2017 / 20:26