How to validate PIS (Social Integration Program) in Javascript?

Asked

Viewed 1,324 times

2

I need a function that validates PIS. I’ve looked and the ones I find don’t work.

I found this:

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;
    }
}  

But I couldn’t make it work.

  • 1

    Link to the official specification: http://www.macoratti.net/alg_pis.htm If you don’t find something ready, you can implement on top of that.

  • 2

    Karina, the website algorithm @Haroldo_ok provided does exactly what your code does. Besides, I tested your code and it seems to me to be correct (although I can simplify a little). So, could you explain why you couldn’t make it work? What problem exactly occurred?

1 answer

-3

Hi, follow the corrected code, only run in JAVA.

public class ValidaPIS {
public static void main(String[] args) {
    Integer i = ValidaPIS("12121212124");
    if(i==0)
        System.out.println("PIS OK");
    else
        System.out.println("PIS INVALIDO");
}

public static Integer ValidaPIS(String numeroPIS) {

    String multiplicadorBase = "3298765432";

    Integer iSoma = 0;
    Integer iResto = 0;
    Integer iDV = 99;

    Integer multiplicando = 0;
    Integer multiplicador = 0;
    Integer iDVInformado = 99;

    Integer i;
    Integer iRet = 0;

    // Retira a mascara
    numeroPIS = numeroPIS.replaceAll("[^\\d]", "");

    if (numeroPIS.length() != 11 || numeroPIS == "00000000000")
        iRet = iRet--;
    else {
        for(i = 0; i < 10; i++) {
            multiplicando = Integer.parseInt(numeroPIS.substring(i, i+1));
            multiplicador = Integer.parseInt(multiplicadorBase.substring(i, i+1));

            iSoma += multiplicando * multiplicador;
        }

        iResto = iSoma % 11;
        if(iResto > 0)
            iDV = 11 - iResto;
        else
            iDV = 0;

        iDVInformado = Integer.parseInt(numeroPIS.substring(10,11));
        if(iDV != iDVInformado)
            iRet--;
    }
    return iRet;
}

}

  • 1

    But the question is about Javascript, not Java :(

  • It’s nice the effort to try to help, but if you don’t know how to do it in JS, you could at least explain the logic you used to correct it, then maybe it’s for the author. If you explain the points you have a problem and how you solved it, leaving Java as an example, MAYBE save the answer (it will depend on the understanding of the community).

Browser other questions tagged

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