PC Review


Reply
Thread Tools Rating: Thread Rating: 2 votes, 1.00 average.

Custom validator not preventing processing

 
 
Steveaux
Guest
Posts: n/a
 
      1st Aug 2008
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
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      2nd Aug 2008
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" <(E-Mail Removed)> wrote in message
news:479e32f6-5e71-4cb1-8367-(E-Mail Removed)...
> 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



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Validator =?Utf-8?B?SmltIEhlYXZleQ==?= Microsoft ASP .NET 1 21st Jan 2005 12:34 AM
Custom Validator =?Utf-8?B?Vmk=?= Microsoft ASP .NET 1 30th Oct 2004 01:30 AM
Custom Validator Andre Microsoft ASP .NET 1 4th May 2004 02:51 PM
Preventing Key/Mouse Events from Interruping Processing Bill Henning Microsoft VB .NET 5 8th Jul 2003 10:41 PM
Preventing Key/Mouse Events from Interruping Processing Bill Henning Microsoft C# .NET 5 8th Jul 2003 10:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:04 AM.