Inputmask() function for Cpf and cnpj

Asked

Viewed 17,712 times

1

I’m using the plugin Inputmask, the following JS code

$("input[id*='cpfcnpj']").inputmask({
    mask: ['999.999.999-99', '99.999.999/9999-99'],
    keepStatic: true
});

Man input is taking the mask of Cpf, but is locked into it. When it reaches the 11 digits, it does not enable the cnpj.

  • 1

    Hello Matheus, could edit your question and add which plugin you are using?

  • Hello Mathes, all right? You don’t need to change the title stop SOLVED, just accept the answer that helped you by clicking on sure sign below the score. Do not forget that you can also vote (arrow up) on the answers that were useful to you. Remembering that you can always visit the [tour] to better understand the sites.

  • Thank you Randrade, I am not very acotumado to use, still learning, but thank you very much for the information

2 answers

8

I didn’t quite understand your problem. Using this library of Inputmask, your code works perfectly.

See an example of it below.

$("input[id*='cpfcnpj']").inputmask({
  mask: ['999.999.999-99', '99.999.999/9999-99'],
  keepStatic: true
});
<script src="https://code.jquery.com/jquery-1.10.0.min.js"></script>
<script src="https://rawgit.com/RobinHerbots/Inputmask/3.x/dist/jquery.inputmask.bundle.js"></script>


<input id="cpfcnpj" placeholder="CPF ou CNPJ" />

  • I don’t know what’s going on then

  • 1

    It worked, it was the version of Inputmask that I was using, I saw that in your example ai ta using the 3, I was using the 2, thanks

2

Well, you don’t need a plugin for that:

function mascaraMutuario(o,f){
    v_obj=o
    v_fun=f
    setTimeout('execmascara()',1)
}

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

function cpfCnpj(v){

    //Remove tudo o que não é dígito
    v=v.replace(/\D/g,"")

    if (v.length <= 14) { //CPF

        //Coloca um ponto entre o terceiro e o quarto dígitos
        v=v.replace(/(\d{3})(\d)/,"$1.$2")

        //Coloca um ponto entre o terceiro e o quarto dígitos
        //de novo (para o segundo bloco de números)
        v=v.replace(/(\d{3})(\d)/,"$1.$2")

        //Coloca um hífen entre o terceiro e o quarto dígitos
        v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2")

    } else { //CNPJ

        //Coloca ponto entre o segundo e o terceiro dígitos
        v=v.replace(/^(\d{2})(\d)/,"$1.$2")

        //Coloca ponto entre o quinto e o sexto dígitos
        v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3")

        //Coloca uma barra entre o oitavo e o nono dígitos
        v=v.replace(/\.(\d{3})(\d)/,".$1/$2")

        //Coloca um hífen depois do bloco de quatro dígitos
        v=v.replace(/(\d{4})(\d)/,"$1-$2")

    }

    return v
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>

<input type="text" class="form-control"  id="user_cpf" name="user_cpf" placeholder="" maxlength="18" onkeypress='mascaraMutuario(this,cpfCnpj)' onblur='clearTimeout()' required data-error="Informe seu CPF">

Browser other questions tagged

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