Skip to content

Instantly share code, notes, and snippets.

@jrmarqueshd
Created November 19, 2019 18:59
Show Gist options
  • Save jrmarqueshd/42d8c8e7eb7041bcefd0562ccbd433ca to your computer and use it in GitHub Desktop.
Save jrmarqueshd/42d8c8e7eb7041bcefd0562ccbd433ca to your computer and use it in GitHub Desktop.
const input = document.getElementById("input");
function telephone(v) {
v = v.replace(/\D/g, "");
v = v.replace(/^(\d\d)(\d)/g, "($1) $2");
v = v.replace(/(\d{4})(\d)/, "$1-$2");
return v
}
input.addEventListener("input", () => {
let value = telephone(input.value);
input.value = value;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment