Skip to content

Instantly share code, notes, and snippets.

@cave2006
Last active August 2, 2023 12:32
Show Gist options
  • Save cave2006/bee57764aaa011d0c9922959b54b38f3 to your computer and use it in GitHub Desktop.
Save cave2006/bee57764aaa011d0c9922959b54b38f3 to your computer and use it in GitHub Desktop.
phone mask script
<label for="phone">Phone: +7</label>
<input type="text" data-js="input" id="phone" />
const input = document.querySelector('[data-js="input"]')
input.addEventListener('input', handleInput, false)
function handleInput (e) {
e.target.value = phoneMask(e.target.value)
}
function phoneMask (phone) {
return phone.replace(/[^0-9]/g, '').substr(-10, 10);
}
body {
padding: 30px
}
input {
font-size: 20px
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment