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

Renan Cambre
Kilo Contributor

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,

6 REPLIES 6

A função abaixo valida corretamente o CPF.

Para testar, atribua CPFs válidos ou inválidos na variável "value".

 

 

function removeSpecialCharacters(str) {
var result = str.replace(/[^a-z0-9]/gi, "");
return result;
}

var value = "111.333.444-55";

var cpfReplaced = removeSpecialCharacters(value.toString().trim());
var cpfLength = cpfReplaced.length;

function checkValidCPF(paramCheck, paramLength) {

try {
var cpf = this.getParameter("sysparm_check") || paramCheck;
var cpfLength = this.getParameter("sysparm_length") || paramLength;

/* Valida CNPJs não preenchidos */
if(cpf == "" || cpf == null) {
gs.print("CPF vazio ou não preenchido.");
}
else {
cpf = cpf.toString();

/* Verifica quantidade de caracteres padrão de um CPF */
if (cpfLength != 11) {
gs.print("CPF com tamanho(quantidade de caracteres) divergente.");
}

/* Elimina inválidos recorrentes */
if (cpf == "00000000000" || cpf == "11111111111"
|| cpf == "22222222222" || cpf == "33333333333"
|| cpf == "44444444444" || cpf == "55555555555"
|| cpf == "66666666666" || cpf == "77777777777"
|| cpf == "88888888888" || cpf == "99999999999") {
gs.print("CPF inválido.");
}

var soma;
var resto;
soma = 0;

for (i=1; i<=9; i++) {
soma = soma + parseInt(cpf.substring(i-1, i)) * (11 - i);
resto = (soma * 10) % 11;
}

if ((resto == 10) || (resto == 11)) {
resto = 0;
if (resto != parseInt(cpf.substring(9, 10)))
gs.print("CPF inválido.");

}
soma = 0;
for (i = 1; i <= 10; i++) {
soma = soma + parseInt(cpf.substring(i-1, i)) * (12 - i);
resto = (soma * 10) % 11;
}

if ((resto == 10) || (resto == 11))
resto = 0;

if (resto != parseInt(cpf.substring(10, 11)))
gs.print("CPF inválido.");

gs.print("CPF válido.");
}
}
catch (e) {
gs.print("Internal Server Error.");
}
}

checkValidCPF(cpfReplaced, cpfLength);

Everson Alves
Tera Contributor

Consegui realizar a validação. 

 

function valida_cpf(par_cpf) {
var cpf, numeros, digitos, soma, i, resultado, digitos_iguais;
cpf = par_cpf;
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;

}

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

if (valida_cpf(newValue))
alert('CPF Válido');
else
alert('CPF Inválido');

}