Você está em boa companhia com suporte incrível 🤝

Máscara de input para telefone

Aplica máscara de telefone brasileiro (XX) XXXXX-XXXX em tempo real com JavaScript puro.

Time Full Services Time Full Services
JavaScript
function mascaraTelefone(input) {
  input.addEventListener('input', function () {
    let v = this.value.replace(/D/g, '').substring(0, 11);

    if (v.length > 10) {
      v = v.replace(/^(d{2})(d{5})(d{4})$/, '($1) $2-$3');
    } else if (v.length > 6) {
      v = v.replace(/^(d{2})(d{4})(d*)$/, '($1) $2-$3');
    } else if (v.length > 2) {
      v = v.replace(/^(d{2})(d*)$/, '($1) $2');
    }

    this.value = v;
  });
}

// Uso:
mascaraTelefone(document.getElementById('telefone'));

Seja PRO.

Tenha acesso a snippets de código premium — PHP, JavaScript, CSS e HTML prontos para usar em seus projetos.

Conhecer o plano Pro →