In ASP.NET a RequiredFieldValidator
still validates a textbox control even if I disabled it on the client-side. I wanted to get rid of this behavior by using a CustomValidator
. In the validation function I check if the text box is enabled. If that is the case I set the validator to valid.
function ValidateTextBox(sender, args) {
var $textbox = $('#txtFoo')
args.isValid = $textbox.prop('disabled') || $textbox.val();
}
This works fine on the client-side. The problem is, that I changed the state of the text box on the client-side using JavaScript. The server therefore does not know about the changed state (that the textbox control is disabled now). So when the ServerValidate
event of the custom validator is raised (by calling Page.Validate
for example) I cannot check whether the text box is disabled or not. Because the server does not know about this the Enabled
property is always set to true, no matter what I do on the client-side.
How can I disable validation for controls when they are disabled or tell the server the state of the text box during Postback
?
Thanks for your time.
Instead of making 'disabled'
try to set textbox to 'ReadOnly'
mode.
You can Enable
or Disable
your RequiredFieldValidator
at the time of you are disabling the textbox using ValidatorEnable
JavaScript method
(reference)