customValidator doesn't work

M

Mr. x

Hello,

I have some code as follows.
It seems that customValidator doesn't work - because i don't get any
message.

In the body :
<asp:table>

<asp:TableRow>
<asp:TableCell>
<asp:RequiredFieldValidator
ControlToValidate="a_email"
Text="*"
runat="server" />
</asp:TableCell>
<asp:TableCell>email:</asp:TableCell>
<asp:TableCell><asp:TextBox id="a_email" runat="server"
/></asp:TableCell>
</asp:TableRow>

<asp:TableRow>
<asp:TableCell>
<asp:CustomValidator
ControlToValidate="a_email"
OnServerValidate="checkEmail"
Text="Illegal"
ErrorMessage="Illegal"
runat="server" />
</asp:TableCell>
</asp:TableRow>

</asp:Table>
....

In the script :

<script runat = "server">
Sub checkEmail(source As object,args As ServerValidateEventArgs)
args.IsValid=false 'just testing - always false, but even this doesn't
work.
End sub
</script>
....

What is wrong in my code ?

Thanks :)
 
M

Morgan

Could be you're using <asp:Table>, which is server side, not client side.
Change it to a standard HTML <table> and the validator should work.
 
S

Scott M.

Your codes shows that you are using a RequiredFieldValidator not a
CustomValidator as this post's subject implies. Which is it?
 
M

Morgan

There is one of each... He must've figured it out as he hasn't replied to
either post.
 
M

Mr. x

There is one of each... He must've figured it out as he hasn't replied to
either post.


Well,
It's good somebody notice that I have both RequiredFieldValidator and
CustomValidator.
(I have checked also without RequiredFieldValidator - the same result).

Still, I didn't resolve the problem, and I am still waiting for kind a
solution, please.

Thanks :)
 
M

mikeb

Mr. x said:
Well,
It's good somebody notice that I have both RequiredFieldValidator and
CustomValidator.
(I have checked also without RequiredFieldValidator - the same result).

Still, I didn't resolve the problem, and I am still waiting for kind a
solution, please.

Your code as originally posted seems to work fine for me - I did have to
add a few overhead-type tags to get ASP.NET to accept the file (@Page,
the form tag, etc.), but otherwise I simply pasted in what you posted.

Here's exactly what I placed into an IIS application directory in test.aspx:
=======================================================================
<%@ Page language="VB" debug="false" %>

<html>
<head>
</head>
<body>
<form runat="server">
<asp:table>

<asp:TableRow>
<asp:TableCell>
<asp:RequiredFieldValidator
ControlToValidate="a_email"
Text="*"
runat="server" />
</asp:TableCell>
<asp:TableCell>email:</asp:TableCell>
<asp:TableCell><asp:TextBox id="a_email" runat="server"
/></asp:TableCell>
</asp:TableRow>

<asp:TableRow>
<asp:TableCell>
<asp:CustomValidator
ControlToValidate="a_email"
OnServerValidate="checkEmail"
Text="Illegal"
ErrorMessage="Illegal"
runat="server" />
</asp:TableCell>
</asp:TableRow>

</asp:Table>

<script runat = "server">
Sub checkEmail(source As object,args As ServerValidateEventArgs)
args.IsValid=false 'just testing - always false
End sub
</script>

</form>
</body>
</html>
=======================================================================

If you still have the problem with this exact aspx file, you might want
to run a repair installation for your .NET Framework.
 
E

Eitan

mikeb said:
Mr. x wrote:

Hello,
I am Eitan, and I use sometimes Mr. X as nick-name in newsgroups.


You need to test for the validity of the page in your
sendContactMessage() button click handler.

Put the following lines at the top of sendContactMessage():

If Not Me.IsValid Then
Return
End If

and I think you'll get the behavior you want.

I did as you said
If Not Me.IsValid Then
Return
End If
)
and now it's almost O.K.
When everythiing is typed correctly I get no message - that's OK.

When the e-mail is typed wrong,
I get the message of ValidationSummary :
Fields with astarisks are must.

I don't want to get this message in every case my input is wrong.
How can I do that ?

Look at my code for :
<asp:ValidationSummary
HeaderText="Fields with astarisks are must"
DisplayMode="BulletList"
EnableClientScript="true"
runat="server"/>

Thanks :)
 
M

mikeb

Eitan said:
Hello,
I am Eitan, and I use sometimes Mr. X as nick-name in newsgroups.






I did as you said


)
and now it's almost O.K.
When everythiing is typed correctly I get no message - that's OK.

When the e-mail is typed wrong,
I get the message of ValidationSummary :
Fields with astarisks are must.

I don't want to get this message in every case my input is wrong.
How can I do that ?

Look at my code for :
<asp:ValidationSummary
HeaderText="Fields with astarisks are must"
DisplayMode="BulletList"
EnableClientScript="true"
runat="server"/>

Thanks :)

Well, the HeaderText property of the ValidationSummary control is just
that - a header that's displayed whenever the validation summary is
displayed. You should set it to a more generic message, or you could
dynamically set it in a Page_Load handler that examines all the
validation controls in the Page.Validators collection and see which ones
failed to create an appropriate message.

But it would probably be simpler to have HeaderText be a generic header,
and set the ErrorMessage and Text properties for the various validators
so that appropriate messages are displayed in the appropriate locations.
 
E

Eitan

mikeb said:
Well, the HeaderText property of the ValidationSummary control is just
that - a header that's displayed whenever the validation summary is
displayed. You should set it to a more generic message, or you could
dynamically set it in a Page_Load handler that examines all the
validation controls in the Page.Validators collection and see which ones
failed to create an appropriate message.
Can you give me some examples of page_load, please ?
But it would probably be simpler to have HeaderText be a generic header,
and set the ErrorMessage and Text properties for the various validators
so that appropriate messages are displayed in the appropriate locations.

I'll check this out, but ...
What generic, i.e ?
Is "There was an error on the following fields" good enough ?
Are there any examples for that ?

What if I several fields that are required fields - for them I want one
generic message.
and two fields that are not required, but not written correctly
(CustomValidator return false) - for them I want each one to have one
message at summary.
How can I do that ?

Thanks :)
 
E

Eitan

N.B.
Another thing, is when I do customValidator, I see that there is not even a
single message for customValidator.


What I did :
<asp:TableRow>
<asp:TableCell ColumnSpan = "3">
<asp:CustomValidator
ControlToValidate="a_email"
OnServerValidate="checkEmail"
Text="my text"
ErrorMessage="my message"
runat="server" />
</asp:TableCell>
</asp:TableRow>

and in the function
checkEmail(source As object,args As ServerValidateEventArgs)
Sub checkEmail(source As object,args As ServerValidateEventArgs)
args.IsValid=false
End Sub


....
and in the eror summary I don't get a message for email - Why ?

Thanks :)
 

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

Top