Last active
August 9, 2024 01:16
-
-
Save migtibincoski/21bf596fbe37baaef94fb0f7e0a7731c to your computer and use it in GitHub Desktop.
Máscara de CPF aplicada a um input em HTML
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
<!DOCTYPE html> | |
<html lang="pt-BR"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Máscara de CPF</title> | |
</head> | |
<body> | |
<form> | |
<label for="cpf">CPF:</label> | |
<input type="text" id="cpf" maxlength="14" oninput="mascaraCPF(this)"> | |
</form> | |
<script src="mascaraCPF.js"></script> | |
</body> | |
</html> |
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
function mascaraCPF(campo) { | |
campo.value = campo.value.replace(/\D/g, "") | |
.replace(/(\d{3})(\d)/, "$1.$2") | |
.replace(/(\d{3})(\d)/, "$1.$2") | |
.replace(/(\d{3})(\d{1,2})$/, "$1-$2"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment