Gostaria de um script para validar o campo CPF / CNPJ para um Formulário para o Brasil. Obrigado.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2017 05:50 AM
Boa tarde,
Gostaria de uma ajuda para um script validador de CPF / CNPJ para um campo no ServiceNow.
Estou tentando elaborar um script para validar os dígitos do CPF / CNPJ, com base no exemplo abaixo feito na Linguagem de programação JAVASCRIPT,
mas não estou tendo muito sucesso.
Caso alguém tenha um, por favor me ajude.
<script>
function TestaCPF(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;
}
Muito obrigado,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2017 01:53 PM
Renan.
Eu não falo português, mas vou usar o google tradutor para responder.
Não entendi bem a pergunta. Você precisa de ajuda com o código JavaScript para validar CPF?
Se este fosse o caso, aqui está um exemplo de Javascript + html funcionando para validade CPF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script>
function valida(){
if(valida_cpf(document.getElementById('cpf').value))
alert('CPF Válido');
else
alert('CPF Inválido');
}
function valida_cpf(cpf){
var numeros, digitos, soma, i, resultado, digitos_iguais;
digitos_iguais = 1;
if (cpf.length < 11)
return false;
for (i = 0; i < cpf.length - 1; i++)
if (cpf.charAt(i) != cpf.charAt(i + 1))
{
digitos_iguais = 0;
break;
}
if (!digitos_iguais)
{
numeros = cpf.substring(0,9);
digitos = cpf.substring(9);
soma = 0;
for (i = 10; i > 1; i--)
soma += numeros.charAt(10 - i) * i;
resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
if (resultado != digitos.charAt(0))
return false;
numeros = cpf.substring(0,10);
soma = 0;
for (i = 11; i > 1; i--)
soma += numeros.charAt(11 - i) * i;
resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
if (resultado != digitos.charAt(1))
return false;
return true;
}
else
return false;
}
</script>
</head>
<input name="cpf" type="text" id="cpf" maxlength="11" />
<input type="button" name="validar" id="validar" value="Validar CPF" onClick="valida()" />
!
CPF Valido
CPF Invalid

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2018 03:52 AM
Obrigado pela ajuda Richard Marcoleta.
Coloquei o Script, porém não está validando a informação no ServiceNow.
Seguem os passos.
1) Criei uma variável do tipo "Single Line Text" com nome de "CPF".
2) Em "Catalog Client Script", criei o Script "Valida formato cpf".
3) No Script "Valida formato cpf", eu preenchi as opções e coloquei o Script.
---------------------------------------script---------------------------------------
function onChange(newValue){
if(valida_cpf(document.getElementById('newValue').value))
alert('CPF Válido');
else
alert('CPF Inválido');
}
function onChange(newValue){
var cpf, numeros, digitos, soma, i, resultado, digitos_iguais;
cpf = newValue;
digitos_iguais = 1;
if (cpf.length < 11)
return false;
for (i = 0; i < cpf.length - 1; i++)
if (cpf.charAt(i) != cpf.charAt(i + 1))
{
digitos_iguais = 0;
break;
}
if (!digitos_iguais)
{
numeros = cpf.substring(0,9);
digitos = cpf.substring(9);
soma = 0;
for (i = 10; i > 1; i--)
soma += numeros.charAt(10 - i) * i;
resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
if (resultado != digitos.charAt(0))
return false;
numeros = cpf.substring(0,10);
soma = 0;
for (i = 11; i > 1; i--)
soma += numeros.charAt(11 - i) * i;
resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
if (resultado != digitos.charAt(1))
return false;
return true;
}
else
return false;
}
------------------------------------------------------------------------------------------
4) Eu faço o teste no formulário, mas o campo "CPF/Natural Persons Registry Number" continua permitindo qualquer dado/informação.
O Meu problema é adaptar no ServiceNow os códigos JavaScript para validação de cpf que eu encontro.
Se alguém puder me ajudar, ficarei muito agradecido, pois deve ser algum detalhe que eu não estou conseguindo entender.
Obrigado a todos,
Att Renan Cambre
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2022 01:02 PM
Opa! tudo bem? eu estou em busca da mesma coisa que voce! Voce conseguiu fazer e poderia me ajudar, por favor?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2019 05:17 AM
Bom dia. Estou com a mesma necessidade de validar campo CPF.
Alguma ajuda?