Last active
November 26, 2024 16:46
-
-
Save desenvolvendositeswp/c14a1534c5336db2df9d31ff38b75f28 to your computer and use it in GitHub Desktop.
Permite o mascaramento de entrada para CPF e Telefones formato br para o filtro JetSmartFilter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Incluir a biblioteca Inputmask --> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/inputmask/5.0.6/inputmask.min.js"></script> | |
<script> | |
// Aplique a máscara para CPF e Telefone quando o DOM estiver carregado | |
document.addEventListener("DOMContentLoaded", function() { | |
// Máscara para CPF | |
var cpfInput = document.querySelector('input[name="cpf"]'); // Ajuste o seletor conforme necessário | |
if (cpfInput) { | |
new Inputmask("999.999.999-99").mask(cpfInput); | |
} | |
// Máscara para Telefone (incluindo DDD) | |
var telefoneInput = document.querySelector('input[name="telefone"]'); // Ajuste o seletor conforme necessário | |
if (telefoneInput) { | |
// Aplicando a máscara e garantindo a limitação de caracteres | |
new Inputmask({ | |
mask: "(99) 99999-9999", | |
placeholder: "_", // Coloca um placeholder para indicar onde o número será inserido | |
clearIncomplete: true, // Limpa valores incompletos se não estiverem completos | |
onBeforePaste: function(pastedValue, opts) { | |
// Remove qualquer caractere que não seja número | |
return pastedValue.replace(/\D/g, ""); | |
} | |
}).mask(telefoneInput); | |
// Limita o número de dígitos para 11 (apenas números) | |
telefoneInput.addEventListener('input', function() { | |
var value = telefoneInput.value.replace(/\D/g, ''); // Remove não números | |
if (value.length > 11) { | |
telefoneInput.value = value.slice(0, 11); // Limita a quantidade de números a 11 | |
} | |
}); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment