i have a disabled custom validator that i want do enable it using jquery.
how can i do that?
the code below works for RequiredFieldValidators but Not for CustomValidators :
ValidatorEnable($('#cvPass'), true);
what is the solution?
EDIT:
i am using server-side validation, not client side.
when we are using client side validation there is no problem about enabling and disabling Custom Validator.
thanks for the nice simple codes of @Josh Mein
thanks in advance
I did not have any issue with what you have tried. Here is a quick example that worked for me:
HTML:
<asp:TextBox runat="server" ID="txt_Test" />
<asp:CustomValidator runat="server" ID="val_Test" ValidateEmptyText="true" ClientValidationFunction="MyValidation" ControlToValidate="txt_Test" ErrorMessage="Test"></asp:CustomValidator>
<asp:Button runat="server" ID="btn_Test" Text="Test" onclick="btn_Test_Click" />
<input type="button" value="disable validator" onclick="disableValidator()" />
<input type="button" value="enable validator" onclick="enableValidator()" />
Javscript:
function enableValidator() {
ValidatorEnable($('[id$=val_Test]')[0], true);
}
function disableValidator() {
ValidatorEnable($('[id$=val_Test]')[0], false);
}
function MyValidation(sender, args) {
if ($('[id$=txt_Test]').val() == "")
args.IsValid = false;
}
Edit: With your new information, I believe I know what is the problem. Before server validation starts, the page is reloaded which causes the validator to be set to disabled because according to the server that was its last known state. This is because validators are not controls whose values are posted back to the server. You could set a hiddenField with a value that could tell the server to validate the control.