Skip to content

Instantly share code, notes, and snippets.

@devalexandre
Created January 2, 2018 18:39
Show Gist options
  • Save devalexandre/049cbbdd5c78dffc2c4907b1e84b28c5 to your computer and use it in GitHub Desktop.
Save devalexandre/049cbbdd5c78dffc2c4907b1e84b28c5 to your computer and use it in GitHub Desktop.
function mtel(v)
{
v=v.replace(/\D/g,""); //Remove tudo o que não é dígito
var res = v.replace(/\D/g,"");
//v=v.replace(/^(\d{2})(\d)/g,"($1) $2"); //Coloca parênteses em volta dos dois primeiros dígitos
//res = "(" + res.substring(0,2) + ")";
if(res.length == 11) {
v = v.replace(/(\d{2})(\d{5})(\d{4})$/, "($1)$2-$3"); //Coloca hífen entre o quarto e o quinto dígitos
console.log(v);
}
else{
v = v.replace(/(\d{2})(\d{4})(\d{4})$/, "($1)$2-$3");
console.log(v);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment