jQuery validation not working when using ASP.NET button
I use jQuery for form validation. When the user doesn't fill out the form he get the message "some fields required". This works if I have a input button, but when I change this to asp:button to postback the webpage and the validation doesn't work.
How can I to solve th开发者_运维问答is problem?
many thanks.
code:
<script type="text/javascript">
$().ready(function()
{
var validator = $("#aspnetForm").bind("invalid-form.validate", function() {
}).validate({
errorElement: "em",
errorContainer: $("#warning, #summary"),
errorPlacement: function(error, element) {
error.appendTo( element.parent("td").next("td") );
},
rules: {
<%=TextBoxCodigo.UniqueID %>: {required: true}
},
messages: {
<%=TextBoxCodigo.UniqueID %>:{required: "Requerido"}
}
});
$('#btnGuardar').click(function () {
var txtUsuario = $('#<%= TextBoxCodigo.ClientID %>').val();
if(validator.form())
{
$.msg({content: "valido"});
}else{$.msg({content: "El formulario tiene " + validator.numberOfInvalids() + " errores.<br> Por favor vea los detalles indicados."});}
});
});
</script>
public void btnGuardar_Click(object sender, EventArgs e)
{
BoClassCodigo boClassCodigo = new BoClassCodigo();
boClassCodigo.CodigoDescripcion = TextBoxCodigo.Text;
boClassCodigo.Sesion = new BoClassSesion();
boClassCodigo.Sesion.Sesiones = (Int64)((Usuario) Session["uSession"]).Sesion;
boClassCodigo.Sesion.Terminal = ((Usuario)Session["uSession"]).Usuario;
string cad = BllClassCodigo.InsertarCodigo(boClassCodigo,conexion);
}
From your question, it seems You should enable the Client Side Validation or Disable the Client Side Validation based on whether you want a postback to happen or not.
It is always recommended to use Validations on both the Client and Server for better security.
I'm guessing your jQuery is looking at the wrong ID.
If you have an asp button like this:
<asp:Button ID="btnOne" runat="server" />
Your jquery code needs to look at:
$('#<%= btnOne.ClientID %>')
精彩评论