I think this is a common problem. I have a form where I show/hide fields dynamically using jQuery, depending on some radio buttons.
I have RequiredFieldValidator's on all the fields, but I don't want them to be triggered if their ControlToValidate is hidden (using jQuery).
Is that possible? Thanks in advance.
EDIT: Here is the solution, thanks to Marek. It might not be very obvious if you have weird clientIDs because of MasterPages
This is the ASPX
<asp:TextBox ID="txtName" runat="server" />
<asp:RequiredFieldValidator ID="vldName" ControlToValidate="txtName" runat="server" ErrorMessage="You must enter Name!" />
...
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
This is the jQuery
$(function() {
$('#ctl00_cphContent_btnSubmit').click(function() {
if (!$('#ctl00_cphContent_txtName').is(':visible'))
ValidatorEnable(ctl00_cphContent_vldName, false);
});
});
Hope it will make someone's life easier
See Question&Answers more detail:os