OnKeyPress

by Renata Oliveira

HTML

<input type="text" id="cpfcnpjCliente" class="cpf">

JavaScript

$('.cpf').mask('00000000000009')

$("#cpfcnpjCliente").keypress(function() {
  var t = $("#cpfcnpjCliente").val().length;
  if (t == 11) {
    $('#cpfcnpjCliente').unmask().mask('#00.000.000-09');
  } else if (t == 14) {
    $('#cpfcnpjCliente').unmask().mask('#9.999.999/9999-99');
  } else {
    $('#cpfcnpjCliente').unmask().mask('00000000000009');
    $("#cpfcnpjCliente").val("");
  }
});