I am asking if it’s possible to make the field required under condition like:
to set a text box required if the other field is not empty otherwise it will be optional.
I am asking if it’s possible to make the field required under condition like:
Share
You can use a CustomValidator for this, but there is something you need to know in order to get it working. The issue is that if you set the
ControlToValidateproperty on the validator, then it will only fire if that control has a value. In your situation, you want to validate it specifically when it does not have a value.To get around this, don’t set the
ControlToValidateproperty on the validator. Then, in your client and server validation handlers you will need to access both of the textboxes directly.In your client validation function you could do something like this:
Your server validation method could look like this:
and your aspx could be like this:
Note that when using this approach, it won’t validate when the (intended) control to be validated looses focus because we haven’t specified that control. So the client side validation will fire when you submit the form, but the postback will be cancelled if the validation fails.