validar campos
Olá, eu tenho 3 campos e preciso que quando clicar em enviar, só envie se os campos estiver com os respectivos dados preenchidos estiver certo, no momento, só esta validando e enviando o campo CPF, ambos JS de de calculo esta funcionando, mas esta validando apenas CPF, se digitar o CPF certo e clicar em enviar ele prossegue para o submit, o campo NF eu queria que o numero minimo e máximo seja 9 números, existe essa possibilidade ? não digitar nada mais e nada menos que 9 números ?!
deixei o link do pen pra teste.
Desde já obrigado.
//calculo C.N.P.J
is_cnpj = function (c) {
var b = [6,5,4,3,2,9,8,7,6,5,4,3,2];
if(/0{14}/.test(c))
return false;
if((c = c.replace(/[^\d]/g,"")).length != 14)
return false;
for (var i = 0, n = 0; i < 12; n += c[i] * b[++i]);
if(c[12] != (((n %= 11) < 2) ? 0 : 11 - n))
return false;
for (var i = 0, n = 0; i <= 12; n += c[i] * b[i++]);
if(c[13] != (((n %= 11) < 2) ? 0 : 11 - n))
return false;
return true;
};
cnpjCheck = function (el) {
document.getElementById('cnpjResponse').innerHTML = is_cnpj(el.value)? '<span style="color:green">válido</span>' : '<span style="color:red">inválido</span>';
if(el.value=='') document.getElementById('cnpjResponse').innerHTML = '';
}//calculo C.P.F
function verificarCPF(strCpf) {
if (!/[0-9]{11}/.test(strCpf)) return false;
if (strCpf === "00000000000") return false;
var soma = 0;
for (var i = 1; i <= 9; i++) {
soma += parseInt(strCpf.substring(i - 1, i)) * (11 - i);
}
var resto = soma % 11;
if (resto === 10 || resto === 11 || resto < 2) {
resto = 0;
} else {
resto = 11 - resto;
}
if (resto !== parseInt(strCpf.substring(9, 10))) {
return false;
}
soma = 0;
for (var i = 1; i <= 10; i++) {
soma += parseInt(strCpf.substring(i - 1, i)) * (12 - i);
}
resto = soma % 11;
if (resto === 10 || resto === 11 || resto < 2) {
resto = 0;
} else {
resto = 11 - resto;
}
if (resto !== parseInt(strCpf.substring(10, 11))) {
return false;
}
return true;
}
function validarNome() {
var strCpf = document.getElementById('cpf1').value;
if (!verificarCPF(strCpf)) {
alert("CPF inválido");
return;
}
document.getElementById('frm').submit();
}
<body>
<div class="corpo">
<form id="form-contato" action="xml.php" method="post">
<div class="content">
<ul style="margin: auto">
<li>
<label for="nf">C.N.P.J</label>
<input id="is_cnpj" type="text" onkeyup="cnpjCheck(this)" placeholder="ex: 26.394.240/0001-01" maxlength="18"><span id="cnpjResponse"></span>
</li>
<li> <label for="cpf">digite aqui seu CPF</label>
<input type="text" id="cpf1" name="txtCPF" placeholder="ex: 070.680.938-68" />
<li><label for="nf">Numero da nota fiscal</label>
<input name="nf" type="text" placeholder="Ex: 001234" onkeydown="limita(this);" onkeyup="limita(this);" style="width: 10%;" style="text-align: center;">
</li>
</ul>
<div style="margin: auto">
<div><button type="button" id="btn1" onclick='javascript:validarNome()'>Enviar</button>
</div>
[https://codepen.io/d4v1fr/pen/ZMqaGO?editors=1010](https://codepen.io/d4v1fr/pen/ZMqaGO?editors=1010)Discussão (3)
Carregando comentários...