2
I have a question about Jquery’s validation. I can use it normally. but I need a validation if the user chooses an option in the "SELECT > OPTION" form, I have two options, "YES", "NO". If the user chooses "YES" will appear another field for him to fill it, but there is my doubt, I am not getting changes the attribution of the fields Rules that was not mandatory, to TRUE, in case he click register and he has not typed in this fields that appeared.
Someone there has a suggestion, but I want to continue using Jquery validate:
Here is my Jquery Code:
$('#form-visitantes').validate({
rules: {
nomecompleto: {
required: true
},
sexo: {
required: true
},
fone: {
required: true
},
email: {
required: true
},
foi_convidado: {
required: false
}
},
messages: {
nomecompleto: {
required: 'Campo Obrigatório'
},
sexo: {
required: 'Campo Obrigatório'
},
fone: {
required: 'Campo Obrigatório'
},
email: {
required: 'Campo Obrigatório'
},
foi_convidado: {
required: 'Campo Obrigatório'
}
},
errorClass: "text-danger",
errorElement: "span",
highlight: function(element, errorClass, validClass) {
$(element).parents('.control-group').addClass('text-danger');
},
unhighlight: function(element, errorClass, validClass) {
$(element).parents('.control-group').removeClass('text-danger');
$(element).parents('.control-group').addClass('success');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.js"></script>
<form class="card shadow-5 form-type-fill" method="POST" action="#" id="form-visitantes">
<h5 class="card-title"><strong>CADASTRAR VISITANTES</strong></h5>
<input type="hidden" name="acao" id="acao" value="inserir">
<div class="card-body">
<h6 class="text-uppercase fs-10 ">Dados Pessoais</h6>
<hr class="hr-sm mb-2 border-info">
<div class="resposta-mensagem slideOutDown"></div>
<div class="row">
<div class="col-lg-12">
<div class="row">
<div class="col-lg-4">
<div class="form-group">
<label>Nome Completo:</label>
<input class="form-control" autocomplete="off" autofocus type="text" name="nomecompleto" id="nomecompleto" value=''>
</div>
</div>
<div class="col-lg-2">
<div class="form-group">
<label>Sexo:</label>
<select class="form-control" data-provide="selectpicker" id="sexo" name="sexo" data-live-search="false">
<option value=''>Genero</option>
<option value='masculino'>Masculino</option>
<option value='feminino'>Feminino</option>
</select>
</div>
</div>
<div class="col-lg-3">
<div class="form-group">
<label>E-mail:</label>
<input class="form-control" autocomplete="off" autofocus type="text" name="email" id="email" value="">
</div>
</div>
<div class="col-lg-3">
<div class="form-group">
<label>Telefone:</label>
<input class="form-control" autocomplete="off" autofocus type="text" name="fone" id="fone" value="">
</div>
</div>
</div>
</div>
</div>
<h6 class="text-uppercase fs-10">Outras Informações</h6>
<hr class="hr-sm mb-2 border-info">
<div class="row">
<div class="col-lg-2">
<div class="form-group">
<label>Frequenta alguma igreja:</label>
<select class="form-control" data-provide="selectpicker" id="frequenta-igreja" name="frequenta_um_igreja">
<option value='nao'>Não</option>
<option value='sim'>Sim</option>
</select>
</div>
</div>
<div class="col-lg-4">
<div class="form-group">
<label>Que Igreja Frequenta:</label>
<input class="form-control cursor-pointer" readonly autocomplete="off" autofocus type="text" name="qual_frequenta" id="frequenta" value="">
<span id="fmsg"></span>
</div>
</div>
<div class="col-lg-2">
<div class="form-group">
<label>Foi Convidado:</label>
<select class="form-control" data-provide="selectpicker" id="foi-convidado" name="foi_convidado">
<option value='nao'>Não</option>
<option value='sim'>Sim</option>
</select>
</div>
</div>
<div class="col-lg-4">
<div class="form-group">
<label>Quem?:</label>
<input class="form-control" readonly autocomplete="off" autofocus type="text" name="quem_convidou" id="quem" value="">
<span id="qmsg"></span>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-4">
<div class="form-group">
<label>Data Visita:</label>
<input class="form-control" data-date-format="dd/mm/yyyy" class="form-control" data-provide="datepicker" autocomplete="off" autofocus type="text" name="data_visita" id="data_visita" value="<?= (isset($dados_edit->nconjuge) == null) ? date('d/m/Y') : $dados_edit->nconjuge ?>">
</div>
</div>
<div class="col-lg-8">
<div class="form-group">
<label>Pedido de Oração:</label>
<input class="form-control" autocomplete="off" autofocus type="text" name="pedidoOracao" id="pedidoOracao" value="<?= (isset($dados_edit->nascimentoconjuge) == null) ? " " : formata_data_brasilia($dados_edit->nascimentoconjuge) ?>">
</div>
</div>
</div>
</div>
<footer class="card-footer text-right">
<a class="btn btn-default" id="cliquei" data-href="<?php echo base_url('visitantes'); ?>">Cancelar</a>
<button class="btn btn-primary" id="btnInserir">Salvar</button>
</footer>
</form>
Welcome to Stackoverflow in English. I edited your question to remove the greetings as we usually keep the text as clean as possible to focus on your scheduling question. If you have questions about the operation, rules and procedures of the site visit the [meta] :)
– Augusto Vasques
Thank you very much!
– user49181
Modify fragment code to use Jquery. Error.
– Augusto Vasques
I don’t understand Augusto, I have little knowledge of Stackoverflow.
– user49181
Press Run. Image of help
– Augusto Vasques
I modified and keeps giving error. I have to call jquery? or the site here already checks this when it is added a JS?
– user49181
Edson, take a test.
– Augusto Vasques
Let’s go continue this discussion in chat.
– user49181