Custom validation

  • Thread starter Thread starter simon
  • Start date Start date
S

simon

I have 2 <asp:textbox controls>:

<asp:textbox id="txt1" Runat="server" ></asp:textbox>

<asp:textbox id="txt2" Runat="server" ></asp:textbox>

User must insert the value either in txt1 OR in txt2, so one of them
shouldn't be empty.

So I use custom validator:

<asp:CustomValidator id="valPhone" Runat="server" ErrorMessage="You must
insert text1 or text2!"
ControlToValidate="txt2"
Display="Dynamic"></asp:CustomValidator>

AND server:

Private Sub valPhone_ServerValidate(ByVal source As Object, ByVal args As
System.Web.UI.WebControls.ServerValidateEventArgs) Handles
valPhone.ServerValidate
If txt1.Text = "" And txt2.Text = "" Then
args.IsValid = False
Else
args.IsValid = True
End If
End Sub

The server validation is fired only if there is some value in txt2 box.If
txt2.text is empty then server validation is not fired.
How can I solve this problem?

I would like also to include client validation.

Thank you,
Simon
 
Try getting rid of the ControlToValidate property. It's not necessary
for a CustomValidator, and in this case, it may be what is preventing
the ServerValidate event from firing.

For client-side validation, use the ClientValidationFunction property
of the CustomValidator.
 
Hi,

If I get rid of the ControlToValidate property then validation function is
executed but when I set
args.isValid=false, nothing happened.
It just pass by like if I set args.isValid=true.
No difference.
Any idea?

Thank you,
Simon
 
Back
Top