#Hello, I'm Arthur from Brazil
1 messages · Page 1 of 1 (latest)
Hey Arthur, how’s it going? There are several ways to handle this natively in Typebot. One approach I personally like is using a variable block in code mode to check whether the CPF is valid or not. Then, you can simply use a condition block to decide what to do with the result.
In your variable block, you can use something like this — or feel free to replace it with your own CPF validation algorithm:
var cpf = {{cpf}}
var Soma = 0
var Resto
var strCPF = String(cpf).replace(/[^\d]/g, '')
if (strCPF.length !== 11)
return false
if ([
'00000000000',
'11111111111',
'22222222222',
'33333333333',
'44444444444',
'55555555555',
'66666666666',
'77777777777',
'88888888888',
'99999999999',
].indexOf(strCPF) !== -1)
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
In the code above, {{cpf}} represents the variable where you stored the user’s CPF value.
If the function returns false, I’d recommend showing a custom error message to make the experience smoother for the user.
Then, immediately send them back to the same question block so they can try again without having to restart the flow. This keeps the interaction natural and avoids frustration.
@crude hull how to make this validation ?
use type SCRIPT after the Text entry set a varible and use condition to test ?
many thanks for this script !
Something along those lines
Many thanks for you explanation !
I´m getting erro on execution ..
It may have some trick to use..
Test replacing {{cpf}} with {{CPF}}
It worked...
I made a little change..
var cpf = {{cpf}}
to
var cpf = "{{CPF}}"
(my var is upper case)