Is There An Elegant Way To Compare A Checkbox And A Textbox Using ASP.NET Validators?

- 1 answer

I have an Asp.Net repeater, which contains a textbox and a checkbox. I need to add client-side validation that verifies that when the checkbox is checked, the textbox can only accept a value of zero or blank.

I would like to use one or more of Asp.Net's validator controls to accomplish this, to provide a consistent display for client side errors (server-side errors are handled by another subsystem).

The Asp:CompareValidator doesn't seem to be flexible enough to perform this kind of complex comparison, so I'm left looking at the Asp:CustomValidator.

The problem I'm running into is that there doesn't seem to be any way to pass custom information into the validation function. This is an issue because the ClientIds of the checkbox and the textbox are unknown to me at runtime (as they're part of a Repeater).

So... My options seem to be:

  1. Pass the textbox and checkbox to the CustomValidator somehow (doesn't seem to be possible).
  2. Find the TextBox through JavaScript based on the arguments passed in by the CustomValidator. Is this even possible, what with the ClientId being ambiguous?
  3. Forget validation entirely, and emit custom JavaScript (allowing me to pass both ClientIds to a custom function).

Any ideas on what might be a better way of implementing this?



I think the best way would be to inherit BaseValidator in a new class, and pass those IDs to your control as attributes. You should be able to resolve the IDs within your validator, without knowing the full client side ID that is generated at runtime. You should get the data validating on the server first, and on the client second.