pages/assets/scripts/phone-masking.js

16 lines
406 B
JavaScript
Raw Normal View History

// Built by perplexity.ai
function maskPhoneNumber(input) {
input.addEventListener("input", function (e) {
var x = e.target.value
.replace(/\D/g, "")
.match(/(\d{0,3})(\d{0,3})(\d{0,4})/);
e.target.value = !x[2]
? x[1]
: "(" + x[1] + ") " + x[2] + (x[3] ? "-" + x[3] : "");
});
}
// Usage
var phoneInput = document.getElementById("phone");
maskPhoneNumber(phoneInput);