0
Good afternoon, I have a form where I built a function to validate unfilled fields. But when I start to fill some fields and leave some empty ones and click on the send button from Submit anyway.
HTML:
<div class="form-group col-md-4">
<label>Fabricante:</label>
<select class="form-control mensalidade required">
<option selected="selected" disabled="disabled">Selecione Algo</option>
<option value="1">algo</option>
</select>
</div>
<div class="form-group col-md-4">
<label for="inputNome">Produto:</label>
<input type="text" placeholder="Produto" id="produto" class="form-control required" >
</div>
<div class="form-group col-md-4">
<label>Modelo:</label>
<input type="text" placeholder="Modelo" id="modelo" class="form-control required">
</div>
<div class="form-group col-md-4">
<label for="inputNome">Número de serie:</label>
<input type="text" placeholder="Numero de serie" id="numeroserie" class="form-control required">
</div>
<div class="form-group col-md-4">
<label for="inputNome">Tensão:</label>
<select class="form-control mensalidade required" id="tensao" required="">
<option selected="selected" disabled="disabled">Selecione a Tensão</option>
<option value="1">110</option>
<option value="2">220</option>
<option value="3">Bivolt</option>
</select>
</div>
<div class="form-group col-md-4">
<label for="inputNome">Versão:</label>
<input type="text" placeholder="Versão do produto (se houver)" id="versao" class="form-control required">
</div>
<div class="form-group col-md-12">
<label>Descrição:</label>
<textarea placeholder="Escreva uma descrição do problema" rows="3" class="form-control required"></textarea>
</div>
<div class="box-footer col-md-12">
<button class="btn btn-warning" type="button" onclick="validarCampos()">Abrir OS</button>
</div>
Javascript
function validarCampos(){
var retorno;
$(".required").each(function() {
if($(this).val() === '' || $(this).val() === null){
$(this).closest(".form-group").addClass("has-error");
retorno = false;
} else {
$(this).closest(".form-group").removeClass("has-error");
retorno = true;
}
});
if(retorno){
document.forms[0].submit();
}
}
Would it only be valid if the field is filled or not? If yes, just use the HTML 'required' attribute.
– Leandro
is that I wanted to drop an error message so that this attribute is not functional for me.
– Thiago Gabriel