# Validar idade

1- Para isso, crie uma mensagem que pergunte a data de nascimento do usuário e salve a resposta como context.data:

![](/files/-Mid-iqif8nAMMW3XMXg)

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

OBS: Esse exemplo serve para datas no formato dd/mm/aaaa
{% endhint %}

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

```

function getAge(dateString) {
    var today = new Date();
    var birthDate = new Date(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    return age;
}


var date=context.data
var formatDate=date && date.split('/').reverse().join('/')
if(getAge(formatDate)>18){
   context.resposta="ok"
}else{context.resposta="nok"}
console.log(getAge(formatDate))


```

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 seja menor que 18 anos:

![](/files/-MicksF605LWVrYPzmrL)

6- Depois crie uma etapa existente e direcione novamente a etapa que pergunta a idade ou para a etapa que você preferir.

![](/files/-MickvFSY3YcePCTxNrR)

7- Prontinho, se o usuário for menor que 18 anos ele vai receber uma mensagem e será encaminhado para perguntar novamente:

![](/files/-MidKtX2DfoBSGZZK9Fj)


---

# Agent Instructions: 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:

```
GET https://help.cosmobots.io/help/outros/dicas/validar-data-de-nascimento.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
