é o seguinte quando eu chamo duas validações (nome; cpf) no submit do form. Se a 1° retornar true, ele ignora a segunda (no caso o cpf), e por outro lado se eu colocar as validações no “onblur” de cada input, ele cria um loop de alerts
segue meu form:
<form name="formCliente" onsubmit="return valida_nome();return validaCPF()" >
<table>
<tr>
<td> Nome:</td> <td> <input id="input_nome" type="text" name="nome" onblur="" required /></td>
</tr>
<tr>
<td> Email:</td> <td> <input type="text" name="email" required /></td>
</tr>
<tr>
<td> CPF: </td> <td><input id="cpf" type="text" name="cpf" onKeyPress="maskCPF(this);return SomenteNumero(event)" maxlength="14" required/></td>
</tr>
<tr>
<td> DataNasc: </td> <td><input type="date" maxlength="9" name="dataNasc" onblur="" required/></td>
</tr>
<tr>
<td> Telefone: </td> <td> <input type="text" name="telefone" id="telefone" onkeypress="mascara(this, '## #####-####');
return SomenteNumero(event)" maxlength="13" required /></td>
</tr>
<tr>
<td> CEP: </td> <td> <input type="text" name="cep" onkeypress="mascara(this, '#####-###');
return SomenteNumero(event)" onblur="" maxlength="9" required /></td>
</tr>
</table>
<br>
<span> <input type="submit" value="Cadastrar" name="acao" style="background-color: green" /> </span>
<a href="index.html"> <input type="button" value="Cancelar" style="background-color: red" /> </a>
<!-Todo form precisa de um submit!!! pois é ele que envia as info ->
</form>
Meu Script:
function validaCPF() {
var cpf = document.getElementById("cpf")
erro = new String;
if (cpf.value.length == 14) {
cpf.value = cpf.value.replace('.', '');
cpf.value = cpf.value.replace('.', '');
cpf.value = cpf.value.replace('-', '');
var nonNumbers = /\D/;
if (nonNumbers.test(cpf.value)) {
erro = "A verificacao de CPF suporta apenas números!";
} else {
if (cpf.value == "00000000000" ||
cpf.value == "11111111111" ||
cpf.value == "22222222222" ||
cpf.value == "33333333333" ||
cpf.value == "44444444444" ||
cpf.value == "55555555555" ||
cpf.value == "66666666666" ||
cpf.value == "77777777777" ||
cpf.value == "88888888888" ||
cpf.value == "99999999999") {
erro = "Número de CPF inválido!"
}
var a = [];
var b = new Number;
var c = 11;
for (i = 0; i < 11; i++) {
a[i] = cpf.value.charAt(i);
if (i < 9)
b += (a[i] * --c);
}
if ((x = b % 11) < 2) {
a[9] = 0
} else {
a[9] = 11 - x
}
b = 0;
c = 11;
for (y = 0; y < 10; y++)
b += (a[y] * c--);
if ((x = b % 11) < 2) {
a[10] = 0;
} else {
a[10] = 11 - x;
}
if ((cpf.value.charAt(9) != a[9]) || (cpf.value.charAt(10) != a[10])) {
erro = "Número de CPF inválido.";
}
}
} else
{
if (cpf.value.length == 0)
return false
else
erro = "Número de CPF inválido.";
}
if (erro.length > 0) {
alert(erro);
cpf.value = "";
cpf.focus();
return false;
}
return true;
}
function valida_nome() {
var filter_nome = /^([a-zA-Zà-úÀ-Ú]|\s+)+$/;
var nome = document.getElementById("input_nome")
if (!filter_nome.test(nome.value)) {
alert("ATENÇÂO! \n Campo Nome não pode conter carácteres especiais!")
nome.value = '';
nome.focus();
return false;
}
return true;
}