0
I am doing a field validation in my project, however I programmed for the empty field error to appear only in the "name" field because I am still working on it. But the error message is showing up in all fields, both for successful validation and for error message.
Javascript:
function myFunction1() {
var valor = document.getElementById("nome").value;
if (valor == null || valor.length == 0 || /^\s+$/.test(valor)) {
$("#nome").parent().parent().attr("class", "form-group2 has-error");
$("#nome").parent().children("span").text("O campo deve ser preenchido").show();
return false;
}
else {
$("#nome").parent().parent().attr("class", "form-group2 has-success");
$("#nome").parent().children("span").text("").hide();
alert("Informações gravadas com sucesso");
}
}
HTML:
<div class="container">
<h2>Cadastro de Indivíduo</h2>
<form role="form">
<div class="form-group">
<label for="id">ID:</label>
<input type="number" class="form-control" disabled="" id="usr">
</div>
<form action="" role="form">
<div class="form-group2">
<label for="nome">Nome:</label>
<input type="text" class="form-control" id="nome">
<span class="help-block"></span>
</div>
<div class="form-group">
<label for="rg">RG:</label>
<input type="number" class="form-control" id=rg>
</div>
<div class="form-group">
<label for="npai">Nome do pai:</label>
<input type="text" class="form-control" id="npai">
</div>
<div class="form-group">
<label for="nmae">Nome da mãe:</label>
<input type="text" class="form-control" id="nmae">
</div>
<div class="form-group">
<label for="nascimento">Data de nascimento:</label>
<input type="date" class="form-control" id="nascimento">
</div>
</form>
<button type="button" id="gravar" class=" btn btn-success" onclick="myFunction1()">Gravar</button>
<button type="button" class="btn btn-primary">Limpar</button>
<button type="button" class="btn btn-danger" onclick="myFunction2()">Excluir</button>
</div>
Where are the "all fields" that are doing the validation?
– Weslley Tavares
@ Weslley Tavares did not put them here because they were not refenciados in js..
– Eduardo Krakhecke
Precisely, if they were not referenced in the JS, how are they being impacted by the validation that occurs only for a certain element? It would be good for you to include your entire page so that someone can (more easily) help you find the solution.
– Weslley Tavares