Update of the question. I need to validate a Select with jquery.validate and bootstrap before registering, otherwise I send the alert. I have the house table and the bet table, in the selected form it is the id of the house table to know the name of the house, and it is that selection that I need to validate. See image.
The problem is seen in image 2, that after selecting the betting house, it continues validating as if nothing had been selected. The code used in the form is this:
<form action="registrar_apuesta.php" method="GET" name="myform" id="myform">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="id_casa">Casa de Apuesta *:</label>
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-home"></i>
</span>
<select id="id_casa" class="form-control" name="id_casa">
<?php
$mysqli = new mysqli('localhost', 'root', 'root', 'apuestas');
?>
<option value="0">-- Seleccionar --</option>
<?php
$query = $mysqli->query("SELECT * FROM casa order by nombre_ca asc");
while ($valores = mysqli_fetch_array($query)) {
echo '<option value="' . $valores['id_casa'] . '">' . $valores['nombre_ca'] . '</option>';
}
?>
</select>
</div>
<span class="help-block" id="error"></span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="valor_apostado" class="form-control-label">Valor Apostado *</label>
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-usd"></i>
</span>
<input type="text" class="form-control" id="valor_apostado" name="valor_apostado" placeholder="" required="true">
</div>
<span class="help-block" id="error"></span>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<input type="submit" class="btn btn-success" value="Registrar">
<button type="button" class="btn btn-danger" data-dismiss="modal"><span class='glyphicon glyphicon-remove'></span> Cerrar</button>
</form>
The code used for the validation is this:
// JavaScript Validation For Registration Page
$('document').ready(function ()
{
var combb = / /;
$.validator.addMethod("combo", function (value, element) {
return this.optional(element) || combb.test(value);
});
var nameregex = /[0-9]/;
$.validator.addMethod("validvalorapos", function (value, element) {
return this.optional(element) || nameregex.test(value);
});
$("#myform").validate({
rules:
{
id_casa: {
required: true,
combo: true,
},
valor_apostado: {
required: true,
validvalorapos: true,
minlength: 4
},
},
messages:
{
id_casa: {
required: "Seleccione una casa de Apuesta",
combo: "Seleccione una casa de Apuesta",
},
valor_apostado: {
required: "Ingrese el Valor Apostado",
validvalorapos: "Sólo se pueden Ingresar Números",
minlength: "Su Nombre es Demasiado Corto"
},
},
errorPlacement: function (error, element) {
$(element).closest('.form-group').find('.help-block').html(error.html());
},
highlight: function (element) {
$(element).closest('.form-group').removeClass('has-success').addClass('has-error');
},
unhighlight: function (element, errorClass, validClass) {
$(element).closest('.form-group').removeClass('has-error').addClass('has-success');
$(element).closest('.form-group').find('.help-block').html('');
},
});
});