Validation of OR of multiple controls

  • Thread starter Thread starter Ananda Sim
  • Start date Start date
A

Ananda Sim

Hi All,

I'm having problems setting up a validation mechanism that covers three
optional <asp:textbox> - Only one of these need to be filled - any one.

The standard RequiredFieldValidator will insist on each one being filled. I
thought I could use the Page_Load handler but if the second postback makes
one validator happy, then other two persist in being not valid so the
..Enabled=False does not seem to be doing what I expect.

Any tips on how to do this?

Thanks
Ananda


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

If IsPostBack Then

If (Me.txtHome.Text = "" And Me.txtWork.Text = "" And Me.txtMobile.Text =
"") Then

Me.HomePhoneValidator.Enabled = True

Me.WorkPhoneValidator.Enabled = True

Me.MobilePhoneValidator.Enabled = True

Me.Validate()

Else

Me.HomePhoneValidator.Enabled = False

Me.WorkPhoneValidator.Enabled = False

Me.MobilePhoneValidator.Enabled = False

Me.Validate()

End If

End If

End Sub
 
Getting nearer...

I found different behaviours with Netscape 4.7x vs IE 6.0.

IE 6.0 did not show whole page postback behaviour whilst Netscape 4.7x did -
so I have for the time being, set the whole page to ClientTarget="downlevel"
and both act the same.

Is there any other way?

Ananda
 
Thanks Hans,

A clarification query -

Hans Kesting said:
The difference in behavior is because for netscape no client-side code is
generated,
so everything is done serverside.

One way to solve this is to use a CustomValidator which will always be
processed server-side.
This validator is specifically for cases where the regular validators can't
be used.
In the server-side validate event you can check if at least (or only) one of
your three
fields is filled.

1. If I use CustomValidator - without using ClientTarget="downlevel", the
event does not seem to trigger at all. i.e. if the textboxes are blank and I
use

Sub CustomValidator.......
if txtFirstname.text = "" AND txtSurname.text="" then........
args.isvalid=false
End Sub

that does not trigger at all. Correct?

Note: CustomValidator (or any validator) will only bind to *ONE*
ControlToValidate. And I have tried CustomValidator for connection to
database to see whether the entered value is in a table. That works fine.
But this approach for this example does not?


2. If I use three RequiredFieldValidators that is ok as well because that
means ALL must textboxes must be filled. Correct?

3. So I
a. create three RequiredFieldValidators
b. programmatically set them to enabled=false or true depending on my
conditions
c. set ClientTarget="downlevel"
so that all validation is serverside. So I lose all clientside validation
that is auto generated for all other controls.
Correct?


Many thanks in advance

Ananda
 
Back
Top