I have an ASP.NET form with three text inputs, one each for "Work Phone", "Home Phone" and "Cell Phone". Each of these text inputs has a RequiredFieldValidator associated with it. I also have a DropDownList where the user can select the preferred phone type.
I want to only require the field that is selected in the DropDownList. For example, if the user selects "Work Phone" from the DropDownList, I want to disable the RequiredFieldValidator for "Home Phone" and "Cell Phone", thereby only making the "Work Phone" field required.
I have a method that enables and disables these validators based on the value of the DropDownList, but I cannot figure out when to call it. I want this method to run before the validation takes place on the page. How would I do that?
When you are using home phone, in dropdown selectedindexchange event, make the required field validators invisible.
like..
if homephone is selected,
if cellphone is selected,
if workphone is selected,
OnChange of the DropDownlist, you will need to register a server-side event handler that enables/disables the validator...
HTH
This is good way to enable and disable server side control validation from jquery event:
OnChange event of the drop down you may have something like this
Check on this url. Section "Client-Side APIs"
http://msdn.microsoft.com/en-us/library/Aa479045#aspplusvalid_clientside
Possible way would be:
AutoPostBack="true"
SelectedIndexChanged
event handler of the DropDownList enable/disable your validatorsCausesValidation="false"
to avoid that the validators block a postback when you change the DropDownList entry.Why not use a CustomValidator in this case? Turning off/on a RequiredFieldValidator could lead to a design issue in the future - I'd stick to using them on fields that are going to be required.