I have a series of questionnaires in which all fields must be answered, the problem is that some inputs are not mandatory, as in the example below:
The input
of type text
is not undergoing validation and it is ignored (jump right to the next question without validating this text input).
Continuing on questão A
, if I enter this text input above the input radio with the value = YES, the text input is validated and the radio input is not validated (jump to the next question).
<!-- INICIO PERGUNTA A -->
<div class="questao questao-A">
<p class="pergunta">A - PG A
</p>
<div class="split-list">
<ul class=" list-group">
<li class="list-group-item list-group-item-success">
<label><input type="radio" name="pgA" value="SIM" /> SIM </label>
<p class="alert alert-success pgA_qual"><label>Qual ? </label>
<input type="text" name="pgA_qual" class="form-control" value=""/>
</p>
</li>
<li class="list-group-item list-group-item-success"><label><input type="radio" name="pgA" value="NÃO" /> NÃO </label></li>
</ul>
</div>
</div>
<!-- FIM PERGUNTA A -->
$(".bloco-atual").find(".questao").each(function(){
var __thisQuestao = $(this);
var __thisElement =__thisQuestao.find(":input");
var thisElementoType = __thisElement.attr("type");
var thisElementoName = __thisElement.attr("name");
// TIPO INPUT
if(thisElementoType === 'radio') {
if($("[name='"+thisElementoName+"']").is(":checked")){
//faz algo
} else {
alert("n checkado");
scrollTop(__thisQuestao);
return false;
}
} else
if(thisElementoType === 'text') {
alert(thisElementoName);
if($("[name='"+thisElementoName+"']").val() === ""){
alert("valor vazio");
scrollTop(__thisQuestao);
return false;
}
}
// TIPO TEXTAREA
var thisName =__thisQuestao.find("textarea").attr("name");
if($("[name='"+thisName+"']").hasClass("obriga")){
if($("[name='"+thisName+"']").val() === ""){
alert("valor vazio");
scrollTop(__thisQuestao);
return false;
}
}
});
The problem jsfiddle follows: link