Click here to Skip to main content
13,198,678 members (48,795 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I am creating a registration form for the users. Here I have written a code for checking Username like this...
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();
                int result = (Int32)cmd.ExecuteScalar();
                if (result >= 1)
                    imgUsr.ImageUrl = "Images/unavailable.png";
                    imgUsr.Visible = true;
                    lblUsr.Text = "Username not available";
                    lblUsr.ForeColor = System.Drawing.Color.Red;
                    imgUsr.ImageUrl = "Images/tick.png";
                    imgUsr.Visible = true;
                    lblUsr.Text = "Available";
                    lblUsr.ForeColor = System.Drawing.Color.Green;

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 8-Jul-12 20:55pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

 write ur code here
vinay7.ra 9-Jul-12 3:06am
Thanks for your answer....
Vani Kulkarni 9-Jul-12 4:08am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Maybe you could write a Custom validator[^] that does the regexp checking and the availability checking?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171020.1 | Last Updated 9 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100