CustomValidator

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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.
 
Back
Top