469,575 Members | 1,194 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

Custom validator not preventing processing

Hi, I'm new to ASP.Net, so this may be something simple that I
forgot. I have a form where a person can create a login. I'm doing
the processing on this myself. The form has a plethora of validators,
including two custom validators. They check whether the user name
exists among the users already registered, and the same with the email
address. Both the validators process, but they don't stop the form
from processing, so I end up with an error inserting a duplicate value
into a unique field. Here's snippets of my code:

<asp:CustomValidator ID="valUserNameExists" runat="server"
OnServerValidate="CheckUserName"
ErrorMessage="User Name already exists"
ControlToValidate="txtUserName" ValidationGroup="RegisterForm">*</
asp:CustomValidator>
<asp:CustomValidator ID="valEmailAddressExists" runat="server"
ControlToValidate="txtEmailAddress"
OnServerValidate="CheckEmailAddress"
ErrorMessage="Email Address already exists"
ValidationGroup="RegisterForm">*</asp:CustomValidator>

Code-behind:

Creates a Sql query that returns the count of records with the user
name or password, then:
If objCommand.ExecuteScalar() 0 Then
args.IsValid = False
lblValidate1.Text = "User Name exists."
Else
args.IsValid = True
lblValidate1.Text = "User Name doesn't exist."
End If

lblValidate1 is just a temporary debugging label that I created. I
commented out the Insert execution in the form processing so I could
use this label to make sure the validators were working at all, and
they were. Here's the button and the processing subroutine
declaration:

<input id="smtRegister" type="submit" value="Register" runat="server"
causesvalidation="true" validationgroup="RegisterForm" />

Public Sub smtRegister_ServerClick(ByVal sender As Object, ByVal e
As System.EventArgs) Handles smtRegister.ServerClick
Aug 1 '08 #1
1 3678
Hi,

if validators update their IsValid correctly (so there's no problem), note
that you must check Page.IsValid in server-side code to prevent
validation-dependant logic from running. CustomValidators can prevent
client-side postbacl if client-sise validation is enabled but user can also
just disable js

See my post:
http://aspadvice.com/blogs/joteke/ar...hts_2900_.aspx

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

"Steveaux" <st***********@gmail.comwrote in message
news:47**********************************@l64g2000 hse.googlegroups.com...
Hi, I'm new to ASP.Net, so this may be something simple that I
forgot. I have a form where a person can create a login. I'm doing
the processing on this myself. The form has a plethora of validators,
including two custom validators. They check whether the user name
exists among the users already registered, and the same with the email
address. Both the validators process, but they don't stop the form
from processing, so I end up with an error inserting a duplicate value
into a unique field. Here's snippets of my code:

<asp:CustomValidator ID="valUserNameExists" runat="server"
OnServerValidate="CheckUserName"
ErrorMessage="User Name already exists"
ControlToValidate="txtUserName" ValidationGroup="RegisterForm">*</
asp:CustomValidator>
<asp:CustomValidator ID="valEmailAddressExists" runat="server"
ControlToValidate="txtEmailAddress"
OnServerValidate="CheckEmailAddress"
ErrorMessage="Email Address already exists"
ValidationGroup="RegisterForm">*</asp:CustomValidator>

Code-behind:

Creates a Sql query that returns the count of records with the user
name or password, then:
If objCommand.ExecuteScalar() 0 Then
args.IsValid = False
lblValidate1.Text = "User Name exists."
Else
args.IsValid = True
lblValidate1.Text = "User Name doesn't exist."
End If

lblValidate1 is just a temporary debugging label that I created. I
commented out the Insert execution in the form processing so I could
use this label to make sure the validators were working at all, and
they were. Here's the button and the processing subroutine
declaration:

<input id="smtRegister" type="submit" value="Register" runat="server"
causesvalidation="true" validationgroup="RegisterForm" />

Public Sub smtRegister_ServerClick(ByVal sender As Object, ByVal e
As System.EventArgs) Handles smtRegister.ServerClick

Aug 2 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mike P | last post: by
2 posts views Thread by Evgueni | last post: by
1 post views Thread by DotNetGruven | last post: by
2 posts views Thread by Dot net work | last post: by
5 posts views Thread by Richard Payne | last post: by
9 posts views Thread by Alex Shirley | last post: by
3 posts views Thread by Andy | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.