Skip to content

Instantly share code, notes, and snippets.

@patricklohn
Created February 7, 2025 02:16
Show Gist options
  • Save patricklohn/c5c70bb8087a95f11b7ff4d58cc35e92 to your computer and use it in GitHub Desktop.
Save patricklohn/c5c70bb8087a95f11b7ff4d58cc35e92 to your computer and use it in GitHub Desktop.
Formatar telefone em JS
function formatarTelefone(value) {
value = value.replace(/\D/g, ""); // Remove tudo que não for número
if (value.length > 10) {
return value.replace(/^(\d{2})(\d{5})(\d{4})$/, "($1) $2-$3");
} else {
return value.replace(/^(\d{2})(\d{4})(\d{0,4})$/, "($1) $2-$3");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment