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