Custom validation

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
 
D

DujHoD

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.
 
S

simon

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
 
Joined
Oct 8, 2008
Messages
2
Reaction score
0
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>
lol...you must be on tension right now...
check this line: ControlToValidate="txt2"
so.....do you get it?
It only to control validate for txt2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top