1
I have a class where I can find out if the CNPJ entered by the user is valid or not.
What I’d like to do, and what I’m not getting, is show the user which are the last two valid digits he should type.
For example:
MessageBox("CNPJ inválido, o Digito Verificador correto seria: "+DV);
I would like to do this for the CNPJ and CPF.
The CNPJ validation method is the following::
public bool ValidaCNPJ(string vrCNPJ)
{
int nrDig;
string CNPJ = vrCNPJ.Replace(".", "");
CNPJ = CNPJ.Replace("/", "");
CNPJ = CNPJ.Replace("-", "");
string ftmt = "6543298765432";
int[] digitos = new int[14];
int[] soma = new int[2];
soma[0] = 0;
soma[1] = 0;
int[] resultado = new int[2];
resultado[0] = 0;
resultado[1] = 0;
bool[] CNPJOk = new bool[2];
CNPJOk[0] = false;
CNPJOk[1] = false;
try
{
for (nrDig = 0; nrDig < 14; nrDig++)
{
digitos[nrDig] = int.Parse(
CNPJ.Substring(nrDig, 1));
if (nrDig <= 11)
soma[0] += (digitos[nrDig] *
int.Parse(ftmt.Substring(
nrDig + 1, 1)));
if (nrDig <= 12)
soma[1] += (digitos[nrDig] *
int.Parse(ftmt.Substring(
nrDig, 1)));
}
for (nrDig = 0; nrDig < 2; nrDig++)
{
resultado[nrDig] = (soma[nrDig] % 11);
if ((resultado[nrDig] == 0) || (resultado[nrDig] == 1))
CNPJOk[nrDig] = (
digitos[12 + nrDig] == 0);
else
CNPJOk[nrDig] = (
digitos[12 + nrDig] == (
11 - resultado[nrDig]));
}
return (CNPJOk[0] && CNPJOk[1]);
}
catch
{
return false;
}
}
Method to validate CPF
public bool ValidaCpf(string cpf)
{
int[] multiplicador1 = new int[9] { 10, 9, 8, 7, 6, 5, 4, 3, 2 };
int[] multiplicador2 = new int[10] { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 };
string tempCpf;
string digito;
int soma;
int resto;
cpf = cpf.Trim();
cpf = cpf.Replace(".", "").Replace("-", "");
if (cpf.Length != 11)
{
return false;
}
tempCpf = cpf.Substring(0, 9);
soma = 0;
for (int i = 0; i < 9; i++)
{
soma += int.Parse(tempCpf[i].ToString()) * (multiplicador1[i]);
}
resto = soma % 11;
if (resto < 2)
{
resto = 0;
}
else
{
resto = 11 - resto;
}
digito = resto.ToString();
tempCpf = tempCpf + digito;
int soma2 = 0;
for (int i = 0; i < 10; i++)
{
soma2 += int.Parse(tempCpf[i].ToString()) * multiplicador2[i];
}
resto = soma2 % 11;
if (resto < 2)
{
resto = 0;
}
else
{
resto = 11 - resto;
}
digito = digito + resto.ToString();
return cpf.EndsWith(digito);
}
I don’t understand why you want to give this information to the user. The check digits serve to confirm that there was no mistake when typing any of the CNPJ digits and not just the last two. What I think you’re suggesting is you want to tell the user to change the last two digits. The CNPJ will be valid but certainly not the user.
– ramaral
Yes @ramaral, I would like to tell the user to change the last two digits, but I thought I would tell him which ones would be the right ones. Of course so the guy will be able to swindle the system with cnpj that doesn’t exist, but it’s just one more option that I would like to make available to him. And learning too!
– Emerson Moraes
@Emersonmoraes, you talk about every question in CNPJ, but your example is CPF, I recommend replacing where you cite CNPJ by CPF, or vice versa.
– Fernando Leal
Sorry @Fernando, I just copied the wrong code. I’ll put the CNPJ tbm!
– Emerson Moraes