> For the complete documentation index, see [llms.txt](https://help.cosmobots.io/help/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.cosmobots.io/help/outros/dicas/validar-cpf.md).

# Validar CPF

1- Para isso, crie uma mensagem que pergunte o CPF do usuário e salve a resposta como usuário CPF:

![](/files/-MickNlR99UEe29UUAym)

{% hint style="info" %}
Caso não tenha esse campo em sua lista de opções crie um novo campo.
{% endhint %}

3- Crie uma etapa JavaScript e insira o seguinte código:

```
var strCPF = user.cpf;

function testaCPF(strCPF) {
	var soma;
	var resto;
	soma = 0;
	if (strCPF == "00000000000") return false;

	for (var i = 1; i <= 9; i++) soma = soma + parseInt(strCPF && strCPF.substring(i - 1, i)) * (11 - i);
	resto = (soma * 10) % 11;

	if ((resto == 10) || (resto == 11)) resto = 0;
	if (resto != parseInt(strCPF && strCPF.substring(9, 10))) return false;

	soma = 0;
	for (i = 1; i <= 10; i++) soma = soma + parseInt(strCPF && 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;
}


if (testaCPF(strCPF) === true) {
	context.resposta = "ok"
} else {
	context.resposta = "nok"

}

console.log(testaCPF && strCPF);
console.log(context.resposta);
```

4- Crie um Filtro, e no filtro coloque a seguinte condição e salve:

![](/files/-MickpMn54vAEAVy7BOB)

5- Do lado não do filtro, insira a mensagem que deve ser enviada ao usuário caso ele informe o CPF incorreto:

![](/files/-MicksF605LWVrYPzmrL)

6- Depois crie uma etapa existente e direcione novamente a etapa que pergunta o CPF.

![](/files/-MickvFSY3YcePCTxNrR)

7- Prontinho, se o CPF do usuário estiver incorreto ele vai receber a mensagem informando.

![](/files/-MiclNDHZqITQ6pqQ4qd)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://help.cosmobots.io/help/outros/dicas/validar-cpf.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
