Text field formatting for PIS/PASEP

Asked

Viewed 3,696 times

3

Actually it’s not a question but a help. I’ve been researching PIS/PASEP mask and I couldn’t find so it was the way to implement.

I’ll put the answer below.

Hugs.

  • How so Cesar?

  • I added the javascript code below R.Santos.

  • What is the format of Pis/pased? could you put that in the question.

  • 1

    The format is 000.00000.00.0 with 14 positions formatted and 11 without formatting. hug.

1 answer

8

Follows below necessary functions.

//Main function

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

//Execution function of 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
}

//Function for validation of Pispasep number

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 mask 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: Don’t 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!

  • but whether it’s valid or not?

  • I’ll add the validation part.

  • Leo Caracciolo just added the validation. I hope it helps.

  • Very good, Cesar Rabelo L JR, will certainly help a lot!!!

  • Make an executable code here! I think it will look cool. + 1 think it will be of great help to staff.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.