I have a registration form, which the person selects if it is an Individual or Legal, and the fields to fill when I select and have a Jquery script that does the validation so that it is mandatory to select and fill these fields after the selection.
If Individuals, you will see CPF (mandatory) and RG (mandatory);
If Legal Entity, appears Corporate Name (mandatory), CNPJ (mandatory) and I.E;
Everything is working perfectly, but due to the validation, I need to choose for example Pessoa Física
the fields of the legal entity would have the html disabled="disabled"
or some function that would leave the field disabled. Because the way it is, even though it is hidden the validation script tries to validate it, thus returning error.
Check out the current script .
Note: To test validation, you must click to fill in the field Email
.
HTML:
<div id="custom-field1" class="form-group custom-field" data-sort="3" style="display: block;">
<label class="col-sm-2 control-label">Tipo de Pessoa</label><br><br>
<label>
<input type="radio" name="custom_field[account][1]" id="id-custom_field-account-1-3" value="3">Pessoa Física</label><br>
<label>
<input type="radio" name="custom_field[account][1]" id="id-custom_field-account-1-4" value="4">Pessoa Jurídica</label>
</div><br>
<div id="div-custom-field4" class="form-group custom-field" data-sort="4" style="display: none;">
<label class="col-sm-2 control-label" for="input-custom-field4">Razão Social</label><br>
<input type="text" name="custom_field[account][4]" value="" placeholder="Razão Social" id="input-custom-field4" class="form-control">
<label for="label-custom-field4" id="campo-obrigatorio" style="display:none">Campo obrigatório</label>
</div>
<div id="div-custom-field5" class="form-group custom-field" data-sort="5" style="display: none;"><br>
<label class="col-sm-2 control-label" for="input-custom-field5">CNPJ</label><br>
<input type="text" name="custom_field[account][5]" value="" placeholder="__.___.___/____-__" id="input-custom-field5" class="form-control cpf_cnpj" autocomplete="off">
<label for="label-custom-field5" id="campo-obrigatorio" style="display:none">Campo obrigatório</label>
</div>
<div id="div-custom-field6" class="form-group custom-field" data-sort="6" style="display: none;"><br>
<label class="col-sm-2 control-label" for="input-custom-field6">I.E</label><br>
<input type="text" name="custom_field[account][6]" value="" placeholder="I.E" id="input-custom-field6" class="form-control">
<label for="label-custom-field6" id="campo-obrigatorio" style="display:none">Campo obrigatório</label><br><br>
</div>
<div id="div-custom-field3" class="form-group custom-field" data-sort="5" style="display: none;">
<label class="col-sm-2 control-label" for="input-custom-field3">CPF</label><br>
<input type="text" name="custom_field[account][3]" value="" placeholder="___.___.___-__" id="input-custom-field3" class="form-control" autocomplete="off">
<label for="label-custom-field3" id="campo-obrigatorio" style="display:none">Campo obrigatório</label><br><br>
</div>
<div id="div-custom-field2" class="form-group custom-field" data-sort="4" style="display: none;">
<label class="col-sm-2 control-label" for="input-custom-field2">RG</label><br>
<input type="text" name="custom_field[account][2]" value="" placeholder="RG" id="input-custom-field2" class="form-control cpf_cnpj">
<label for="label-custom-field2" id="campo-obrigatorio" style="display:none">Campo obrigatório</label><br><br>
</div>
<label class="col-sm-2 control-label" for="input-email">E-mail</label><br>
<input type="email" name="email" value="" placeholder="E-mail" id="input-email" class="form-control">
PART OF JAVASCRIPT:
$(document).ready(function() {
$('input:radio[name^="custom_field[account][1]"]').on("change", function() {
var chosen = this.checked && this.value == '3';
$("#div-custom-field2, #div-custom-field3").toggle(chosen).find('input').attr('disabled', !chosen);
$("#div-custom-field4, #div-custom-field5, #div-custom-field6").toggle(!chosen).find('input').attr('disabled', chosen);
});
$('[id^="input-custom-field"]:disabled').closest('.form-group.custom-field').hide();
});