Last active
August 2, 2023 12:32
-
-
Save cave2006/bee57764aaa011d0c9922959b54b38f3 to your computer and use it in GitHub Desktop.
phone mask script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<label for="phone">Phone: +7</label> | |
<input type="text" data-js="input" id="phone" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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