Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi
 
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();
                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 8-Jul-12 21:55pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use
 Page.Validate();
 if(Page.IsValid)
{  
 write ur code here
}
  Permalink  
Comments
vinay7.ra at 9-Jul-12 3:06am
   
Thanks for your answer....
Vani Kulkarni at 9-Jul-12 4:08am
   
Correct!
Rate this: bad
good
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?
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 273
1 Maciej Los 255
2 Aajmot Sk 234
3 Richard MacCutchan 200
4 Marcin Kozub 195
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 9 Jul 2012
Copyright © CodeProject, 1999-2014
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