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
If I remember correctly there’s a function called
ValidatorEnable(validatorClientId, isEnabled)that allows you to disable/enable the ASP.NET validators via javascript. You could use jQuery right before your form submit to disable all your invisible validators.There’s some documentation about client side API available from the validators here http://msdn.microsoft.com/en-us/library/aa479045.aspx