Last active
September 27, 2022 22:47
-
-
Save vitorjustin/2a67673fd31ef9f60f70291327ed30a7 to your computer and use it in GitHub Desktop.
Máscara para input CPF somente HTML e JS puro
This file contains hidden or 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
<input type="text" name="cpf" placeholder="CPF" required oninput="cpfMask(this);" pattern="\d{3}\.\d{3}\.\d{3}-\d{2}"> | |
<script> | |
function cpfMask(i) { | |
let v = i.value; | |
let digits = v.replace(/[^0-9]/g, '').substring(0, 11); | |
let formatted = digits; | |
formatted = formatted.replace(/^(\d{3})(\d)/, "$1.$2"); | |
formatted = formatted.replace(/^(\d{3})\.(\d{3})(\d)/, "$1.$2.$3"); | |
formatted = formatted.replace(/^(\d{3})\.(\d{3})\.(\d{3})(\d)/, "$1.$2.$3-$4"); | |
i.value = formatted; | |
return; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment