Skip to content

Instantly share code, notes, and snippets.

@migtibincoski
Last active August 9, 2024 01:16
Show Gist options
  • Save migtibincoski/21bf596fbe37baaef94fb0f7e0a7731c to your computer and use it in GitHub Desktop.
Save migtibincoski/21bf596fbe37baaef94fb0f7e0a7731c to your computer and use it in GitHub Desktop.
Máscara de CPF aplicada a um input em HTML
<!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>
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