I have set up validation on a .Net form with the JQuery plugin, everything works on page load, but after a post back the validation stops working.
Then if I empty a requiered field and try to submit, when my .Net validators catch the problem client side, then the live validation on the fields starts working again.
Here is a small code sample which reproduce the problem:
<script language="javascript">
$(document).ready(function () {
ValidateElements();
});
function ValidateElements() {
jQuery.validator.messages.required = " ";
jQuery.validator.messages.email = " ";
var rules = {};
rules[$("#<%=TxtInvoiceName.ClientID%>").attr("name")] = {
required: true
};
$('form').validate({
rules: rules,
success: function (label) {
},
errorPlacement: function (label) {
}
});
$("#MainForm").validate().form();
}
</script>
<style>
input.error {
border: 1px solid red;
}
</style>
<form id="MainForm" runat="server">
<div>
<asp:TextBox runat="server" ID="TxtInvoiceName" Text="" />
<asp:RequiredFieldValidator ID="RequiredFieldInvoiceName" runat="server" ErrorMessage=""
Display="Dynamic" ControlToValidate="TxtInvoiceName"></asp:RequiredFieldValidator>
<asp:Label runat="server" ID="LblTxtInvoiceNameValidate" AssociatedControlID="TxtInvoiceName"> </asp:Label>
<asp:Button runat="server" Text="PostBack" OnClick="PostBack" />
</div>
</form>
Hope someone can point me to what im doing wrong.