CustomValidator not working

T

tshad

How do I get my Custom Validator to work? All the other validators work
fine.

Here is the Textbox and Validator:

***********************************************
<asp:textbox id="email" TextMode="SingleLine" Columns="32"
runat="server" />
<asp:RegularExpressionValidator ControlToValidate="email" Text =
"Invalid Email Address!"
ValidationExpression="\S+@\S+\.\S{2,3}" runat="server" />
<asp:RequiredFieldValidator
ControlToValidate="email"
Text="You must enter an email"
runat="server" />
<asp:CustomValidator
ControlToValidate="email"
OnServerVAlidate="ValidateEmail"
Display="Dynamic"
Text="Email already on File"
runat="server" />
*********************************************************************

Here is the ValidateEmail code:
*****************************************************************
Sub ValidateEmail(s as Object,e as ServerValidateEventArgs)
trace.warn("in ValidateEmail")
Dim sResults As String
Dim emailReader As SqlDataReader

Dim ConnectionString as String
=System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_ftsolutions")
Dim objConn as New SqlConnection (ConnectionString)
Dim CommandText as String = "Select email from logon where email = @Email"

Dim objCmd as New SqlCommand(CommandText,objConn)
with objCmd.Parameters
.Add("@Email",SqlDbType.VarChar,45).value = email.text
end with
objConn.Open()

emailReader = objCmd.ExecuteReader
trace.warn("before emailReader.Read")
if emailReader.Read then
trace.warn("not valid")
e.IsValid = false
end If
End Sub
*****************************************************************

I do get to the "not valid trace line".

But it also goes to the insertRecord routine (which is hooked to the
button).

How do I tell it - If there is an email record - don't do the insertRecord
routine:

*******************************************************************
Sub InsertRecord()
trace.warn("inside insertRecord")
Dim sResults As String

Dim ConnectionString as String
=System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_ftsolutions")
Dim objConn as New SqlConnection (ConnectionString)
Dim CommandText as String = "INSERT INTO ftsolutions.dbo.logon
(FirstName,LastName,Email,Password) VALUES
(@FirstName,@LastName,@Email,@Password)"
Dim objCmd as New SqlCommand(CommandText,objConn)

with objcmd.Parameters
.Add("@FirstName",SqlDbType.VarChar,15).value = firstName.text
.Add("@LastName",SqlDbType.VarChar,30).value = lastName.text
.Add("@Email",SqlDbType.VarChar,45).value = email.text
.Add("@Password",SqlDbType.VarChar,20).value = password.text
end with

objConn.Open()
objCmd.ExecuteNonQuery
End Sub
*************************************************************

I set e.IsValid to false - so how do I tell insertRecord to look at it?

Thanks,

Tom.
 
J

Jeffrey Palermo [MCP]

Tshad,
I noticed one thing in your code: you specified a ControlToValidate in
your custom validater. This property should be left off.

Best regards,
Jeffrey Palermo
 
T

tshad

Jeffrey Palermo said:
Tshad,
I noticed one thing in your code: you specified a ControlToValidate in
your custom validater. This property should be left off.

That didn't work.

The same thing happens. With trace on, I can see it going to the validation
function, but it also does the insert, which is where the button sends it.

Also, the books I have show that the ControlToValidate are supposed to be
there for the custom validator. Are there instances where you want it and
others where you don't?

Thanks,

Tom.
 
T

tshad

Patrick.O.Ige said:
Guess u are missing something :-
Go to :-
http://samples.gotdotnet.com/quickstart/aspplus/
and read :- Server Control Form Validation
Enjoy

Found it there.

I didn't see the "IsValid" in the buttons routine.

What I had to do was add to the ModifyRecord_click routine:
*************************************************
Sub ModifyRecord_click(sender as Object, e as EventArgs)

if Not Page.IsValid then
exit sub
end if
***************************************************

and it works fine.

Thanks,

Tom
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top