0
I have a series of questionnaires in which all fields must be answered, the problem is that some inputs are not required, as in the example below:
The input
type text
is not going through validation and it is ignored (skips straight to the next question without validating this text input).
Continuing on the questão A
, if I put this text input above the radio input with the value = YES, the text input is validated and the radio input is not validated (skip 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;
}
}
});
Follow the jsfiddle of the problem: https://jsfiddle.net/6rzn6vjg/
only one question, you cannot use required to facilitate validation?
– haykou