1
I have an input:
<input id="in_cpf_cnpj" name="cpf_cnpj" class="form-control" placeholder="Digite o CNPJ" required autofocus></>
I need to send the value of the same, as parameter, to a function through the Onclick of a button, I’m trying this way, but it doesn’t work.
<button class="btn btn-block bg-red waves-effect" onclick="validaCampo('cnpj', in_cpf_cnpj.value" type="button">Confirmar</button>
the code of the validated function:
function validaCampo(tipo, valor) {
//Tipos: CNPJ,
if (tipo != '') {
//valida cnpj
if (tipo == 'cnpj') {
valor = valor.replace(/[^\d]+/g, '');
debugger;
if (valor == '') return false;
if (valor.length != 14)
return false;
// Elimina CNPJs invalidos conhecidos
if (valor == "00000000000000" ||
valor == "11111111111111" ||
valor == "22222222222222" ||
valor == "33333333333333" ||
valor == "44444444444444" ||
valor == "55555555555555" ||
valor == "66666666666666" ||
valor == "77777777777777" ||
valor == "88888888888888" ||
valor == "99999999999999")
return false;
// Valida DVs
tamanho = valor.length - 2
numeros = valor.substring(0, tamanho);
digitos = valor.substring(tamanho);
soma = 0;
pos = tamanho - 7;
for (i = tamanho; i >= 1; i--) {
soma += numeros.charAt(tamanho - i) * pos--;
if (pos < 2)
pos = 9;
}
resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
if (resultado != digitos.charAt(0))
return false;
tamanho = tamanho + 1;
numeros = valor.substring(0, tamanho);
soma = 0;
pos = tamanho - 7;
for (i = tamanho; i >= 1; i--) {
soma += numeros.charAt(tamanho - i) * pos--;
if (pos < 2)
pos = 9;
}
resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
if (resultado != digitos.charAt(1))
return false;
return true;
}
}
}
Missing close parentesis here:
validaCampo('cnpj', in_cpf_cnpj.value
. You can put it in your codevalidaCampo
? So we can help more to understand what might be failing.– Sergio
@Sergio, I edited the content and added the function, I know I could enter the name of the field inside it, but to follow a pattern, I would like to pass via parameter
– Gleyson Silva
@Sergio, the first option gave error, but the second one worked blz. Thank you!
– Gleyson Silva