Click here to Skip to main content
15,891,733 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi
I am creating a registration form for the users. Here I have written a code for checking Username like this...
C#
protected void txtUsername_TextChanged(object sender, EventArgs e)
        {
            if (txtUsername.Text != string.Empty)
            {
                string strSelect = "SELECT COUNT(*) FROM tblRegisteredUsers WHERE Username = @Username";
                SqlConnection con = new SqlConnection(constr);
                SqlCommand cmd = new SqlCommand(strSelect, con);

                SqlParameter username = new SqlParameter("@Username", SqlDbType.VarChar);
                username.Value = txtUsername.Text.Trim().ToString();
                cmd.Parameters.Add(username);
                con.Open();
                int result = (Int32)cmd.ExecuteScalar();
                con.Close();

                if (result >= 1)
                {
                    imgUsr.ImageUrl = "Images/unavailable.png";
                    imgUsr.Visible = true;
                    lblUsr.Text = "Username not available";
                    lblUsr.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    imgUsr.ImageUrl = "Images/tick.png";
                    imgUsr.Visible = true;
                    lblUsr.Text = "Available";
                    lblUsr.ForeColor = System.Drawing.Color.Green;
                    txtCommonEmail.Focus();
                }
            }
        }


Here the txtUsername field is in AutoPostback-true.

But here my problem is I have put 2 other validations for Username field (Required field Validator(*Enter Username) and Regular Expression Validator(*Please use only letters (a-z), numbers, dot.)). Here the Required field validator is working fine, but when the user enter any wrong format(vin@7 ra) it is dispaying the error msg for few seconds and showing username is available. Please give me some suggestions to this.

The Username availability has to check only once the user enters the correct format that means once the regular expression validation is correct then only it has to check the availability of the user.

Sorry for any errors.
Posted

Use
 Page.Validate();
 if(Page.IsValid)
{  
 write ur code here
}
 
Share this answer
 
Comments
vinay7.ra 9-Jul-12 3:06am    
Thanks for your answer....
Vani Kulkarni 9-Jul-12 4:08am    
Correct!
Maybe you could write a Custom validator[^] that does the regexp checking and the availability checking?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900