CustomValidator

G

Guest

Hi

I need to validate the duplication of username.

Here is the aspx code
[code
<td>User Name:</td><td><asp:textbox id="username" Runat="server"></asp:textbox><asp:requiredfieldvalidator id="RequiredFieldValidatorUserName" runat="server" ErrorMessage="Please input user name!
Display="None" ControlToValidate="username" EnableViewState="False"></asp:requiredfieldvalidator><asp:customvalidator id="CustomValidatorUserName" runat="server" ErrorMessage="Your login name was taken by other, please re-enter!" Display="Dynamic" ControlToValidate="username" EnableClientScript="False"></asp:customvalidator><asp:validationsummary id="ValidationSummary" runat="server" ShowMessageBox="True" ShowSummary="False"></asp:validationsummary
[/code

Here is C# code
[code
private void InitializeComponent(
{
this.CustomValidatorUserName.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.CustomValidatorUserName_ServerValidate)
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click)
this.Load += new System.EventHandler(this.Page_Load)


private void CustomValidatorUserName_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args

string userName
tr

userName = args.Value.Trim()
if(!Bool method to connect to DB with SQL to validate the existence of username

args.IsValid = true
return


catch(Exception) {
args.IsValid = false

[/code

The Bool method contains the sql: select username from usertable where username ='userName

The error is that the ErrorMessage="Your login name was taken by other, please re-enter!" cannot be displayed.

And, the duplicated username data insert to MS SQL 2000 DB.

What is wrong with my code?

Thanks for advice
 
M

Martin Dechev

Hi, Tom,

The best way to find the errors in your code is to debug it. Try doing so.

Greetings
Martin
Tom said:
Hi,

I need to validate the duplication of username.

Here is the aspx code:
Code:
<td>User Name:</td><td><asp:textbox id="username"[/QUOTE]
Runat="server"></asp:textbox><asp:requiredfieldvalidator
id="RequiredFieldValidatorUserName" runat="server" ErrorMessage="Please
input user name!"[QUOTE]
Display="None" ControlToValidate="username"[/QUOTE]
EnableViewState="False"></asp:requiredfieldvalidator><asp:customvalidator
id="CustomValidatorUserName" runat="server" ErrorMessage="Your login name
was taken by other, please re-enter!" Display="Dynamic"
ControlToValidate="username"
EnableClientScript="False"></asp:customvalidator><asp:validationsummary
id="ValidationSummary" runat="server" ShowMessageBox="True"
[QUOTE="ShowSummary="False">"]

Here is C# code:
Code:
private void InitializeComponent()
{
this.CustomValidatorUserName.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.CustomValidatorUse
rName_ServerValidate);
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}

private void CustomValidatorUserName_ServerValidate(object source,[/QUOTE]
System.Web.UI.WebControls.ServerValidateEventArgs args)[QUOTE]
{
string userName;
try
{
userName = args.Value.Trim();
if(!Bool method to connect to DB with SQL to validate the existence of username)
{
args.IsValid = true;
return;
}
}
catch(Exception) {}
args.IsValid = false;
}

The Bool method contains the sql: select username from usertable where username ='userName'

The error is that the ErrorMessage="Your login name was taken by other,
please re-enter!" cannot be displayed.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top