I have a form that sends information to another web page to do an operation on a DB, but I am trying to validate the form first so that it does not send empty fields to the DB, the validation I already have but I could not do to validate first and if there are no empty fields send the information to the BD, since it currently sends even when it is validated but it is because I have a js that has a click event where the form is processed through ajax to the other page and I do not know how to do it first validate and then process, doing tests, comment on the click function and validate you may have to occupy another event but I do not know, this is my code:
$('#btnGuarda').click(function () {
var cargando = $("#carga").html("<center><img class='img-responsive' src='../imagenes/loading.gif'/><center>");
$.ajax({
type: 'POST',
url: '../statussol/procesaConfirmada.vbhtml',
data: $('#enviaConfirmada').serialize(),
beforeSend: function () {
$("#muestraFormulario").hide();
cargando.show().fadeIn();
},
success: function (e) {
cargando.hide();
$('#resultado').hide().html("<blockquote style='background: #f9f9f9; border-left: 10px solid #ccc; margin: 1.5em 10px; padding: 0.5em 10px;'> <p></p><p style='color:green'><i class='glyphicon glyphicon-ok-circle'></i> ¡ Operación exitosa !</p><p></p> </blockquote>").fadeIn(500);
$('#resultado').html(e);
}
});
});
/* VALIDACION A MI INPUT DEL FOMULARIO*/
$('#enviaConfirmada').bootstrapValidator({
message: 'Este valor no es valido',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
txtVendedor: {
validators: {
notEmpty: {
message: 'El # de vendedor es requerido'
}
}
}
}
});
});
my form
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<link href="../css/bootstrap.min.css" rel="stylesheet" />
<link href="../css/bootstrapValidator.min.css" rel="stylesheet" />
<script type="text/javascript" src="../js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../js/bootstrapValidator.min.js"></script>
<script type="text/javascript" src="../js/scriptsConfirma.js"></script>
<script src="../js/validate.js"></script>
</head>
<body>
<!-- div muestra formulario -->
<div id="muestraFormulario">
<!-- formulario -->
<div class="alert alert-info" role="alert"><i class="glyphicon glyphicon-asterisk"></i> Ingrese número</div>
<form id="enviaConfirmada" name="enviaConfirmada" method="post" action="" class="form-horizontal">
<div class="form-group">
<label for="lblSolicitud" class="control-label col-sm-3">N° de solicitud</label>
<div class='input-group col-sm-8'>
<span class="input-group-addon">
<span class="glyphicon glyphicon-info-sign"></span>
</span>
<input type='text' id="txtNumsolicitud" name="txtNumsolicitud" class="form-control" readonly="readonly" />
</div>
<label for="lblSolicitud" class="control-label col-sm-1"></label>
</div>
<div class="form-group">
<label for="lblFecha" class="control-label col-sm-3">Fecha</label>
<div class='input-group col-sm-8'>
<span class="input-group-addon">
<span class="glyphicon glyphicon-info-sign"></span>
</span>
<input type='text' id="txtFecha" name="txtfecha" class="form-control" readonly="readonly" />
</div>
<label for="lblFecha" class="control-label col-sm-1"></label>
</div>
<label class="col-md-3 control-label">N° de vendedor</label>
<div class="col-md-7">
<input type='text' id="txtVendedor" name="txtVendedor" maxlength="4" onkeypress="ponerMayusculas(this);" class="form-control" />
</div>
<div class="form-group">
<label class="control-label col-sm-9"></label>
<div class="col-sm-3">
<button type="button" class="btn btn-success" id="btnGuarda">Guardar</button>
</div>
</div>
</form>
<!-- fin formulario -->
</div>
<!-- div muestra formulario -->
<div id="carga"></div>
<div id="resultado"></div>
</body>
</html>