I am trying to disable required field validator if checkbox is ticked through jquery.
It works fine but on page post back , validator is not disabled and i see required field message.
<asp:CheckBox ID="chkAssociate" Text="Associate / Duplicate Email"
runat="server" TabIndex="-1" />
<asp:TextBox ID="txtAddress" TabIndex="4" Width="220px" TextMode="MultiLine" Rows="3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAddress" runat="server" ControlToValidate="txtAddress" Display="Dynamic" CssClass="required" ErrorMessage="* Required" ValidationGroup="Save" SetFocusOnError="True"></asp:RequiredFieldValidator>
JQUERY
var valAddress = $("[id*=rfvAddress]");
if ($(this).is(':checked'))
{
$("[id*=spAddress]").css('display', 'none');
ValidatorEnable(valAddress[0], false);
}
else
{
$("[id*=spAddress]").css('display', 'inline');
ValidatorEnable(valAddress[0], true);
}
Your issue is in the link you mention; anything changed on the client will not be persisted on the server; you would have to interpret whether the validator is enabled when the page posts back, and set the Enabled property on the server control itself:
if (chkAssociate.Checked) {
rfvAddress.Enabled = False;
}
This should then persist back to the client. Changing enabled status on the client and server may behave differently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With