Use the following code
Reference Bootstrap 4
Follows your form:
<div class="form-row">
<div class="form-group col-md-4">
<label for="txtDataInicio">Data Início:</label>
<select name="cursos_categoria" id="cursos_categoria">
<option>Selecione a categoria de cursos</option>
<option value="Imersão">Imersão</option>
<option value="Aperfeiçoamento">Aperfeiçoamento</option>
<option value="Especialização">Especialização</option>
<option value="Superior">Superior</option>
<option value="Outros">Outros</option>
</select>
</div>
</div>
Using the jquary framework. Validate, create the following JS script:
$(function () {
$.validator.addMethod(
"Curso",
function (value, element) {
if ($("#cursos_categoria").val() === "") {
return false;
} else {
return true;
}
}
);
$("#idFormulario").validate({
onfocusout: function (e) {
this.element(e);
},
onkeyup: false,
highlight: function (element) {
$(element).closest(".form-control").addClass("is-invalid");
},
unhighlight: function (element) {
$(element).closest(".form-control").removeClass("is-invalid");
$(element).closest(".form-control").addClass("is-valid");
},
errorElement: "div",
errorClass: "invalid-feedback",
errorPlacement: function (error, element) {
if (element.parent(".input-group-prepend").length) {
$(element).siblings(".invalid-feedback").append(error);
} else {
error.insertAfter(element);
}
},
rules: {
'Curso': {
required: true
}
},
messages: {
'Curso': {
required: "Selecione o Curso!"
}
}
});
});
Following link of reference: https://coderanch.com/t/554835/languages/jQuery-Validator-select
For all this to work you need to have Jquary Validate references
you can consult the following link Jqueryvalidate
How simple! had done several tests using some examples I saw and none had worked out, thank you very much
– Sérgio Machado
@Sérgiomachado glad everything worked out :)
– Gabriel Rodrigues