
If you're based in Brazil, you might be aware of the CPF, a legal document that's required for various official purposes. In this article, I'll show you how to validate the CPF using JavaScript and even copy it from the memory or input field.
You can also generate a valid CPF to test the code yourself here.
How it works?
We start the page with an interval reading the clipboard memory.
let intervalCpf = setInterval(() => {
navigator.clipboard.readText()
.then(text => {
findValidCPFsInClipboard(text);
})
.catch(err => {
console.error("Error reading clipboard:", err);
});
}, 2000);
To use this code, the end-user needs to accept the browser to read the clipboard. We start the page with an interval reading the clipboard memory.

When the page encounters something on the clipboard, it reads and puts it on the input field.
function findValidCPFsInClipboard(clipboardText) {
const words = clipboardText.split(/\s+/);
for (const word of words) {
let strCPF = word.replaceAll('.', '').replaceAll('-', '');
if (isValidCPF(strCPF)) {
$("#cpf").val(maskCPF(strCPF));
$("#isValid").text("CPF copied from memory is valid!");
$("#isValid").show();
clearInterval(intervalCpf);
return true;
}
$("#isValid").hide();
}
return false;
}
The maskCpf function is called to apply the mask, which is necessary for printing the CPF number. Here's the code used to display the CPF number.
function maskCPF(cpf) {
cpf = cpf.replace(/\D/g, '');
cpf = cpf.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, '$1.$2.$3-$4');
return cpf;
}
Below is the code used to validate the CPF number.
function isValidCPF(strCPF) {
var Soma;
var Resto;
Soma = 0;
if (strCPF == "00000000000") return false;
for (i = 1; i <= 9; i++) {
Soma = Soma + parseInt(strCPF.substring(i - 1, i)) * (11 - i);
}
Resto = (Soma * 10) % 11;
if (Resto == 10 || Resto == 11) Resto = 0;
if (Resto != parseInt(strCPF.substring(9, 10))) return false;
Soma = 0;
for (i = 1; i <= 10; i++) {
Soma = Soma + parseInt(strCPF.substring(i - 1, i)) * (12 - i);
}
Resto = (Soma * 10) % 11;
if (Resto == 10 || Resto == 11) Resto = 0;
if (Resto != parseInt(strCPF.substring(10, 11))) return false;
return true;
}
Here is it in action.

There's also a second script that reads the memory only when the input field is focused.
$("#cpf2").focusin((e) => {
navigator.clipboard.readText()
.then(text => {
const words = text.split(/\s+/);
for (const word of words) {
let strCPF = word.replaceAll('.', '').replaceAll('-', '');
if (isValidCPF(strCPF)) {
$("#cpf2").val(maskCPF(strCPF));
}
}
})
.catch(err => {
console.error("Error reading clipboard:", err);
});
});
Conclusion
Filling in the CPF number manually can be tedious, especially when it's already stored in your memory. With this code, you can automate the process of copying and validating the CPF number, saving you time and effort.

Henry EdwardPosted Jul 9, 2026, 1:44 PM
Nice approach. Reading the clipboard and validating the CPF before filling the field is a great time saver for internal tools. During development I also use https://gerador-br.com/ to quickly generate valid test CPFs before checking the validation logic.