关于验证和检查用户名可用性. [英] About Validation & Checking Username Availability.

查看:97
本文介绍了关于验证和检查用户名可用性.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在为用户创建注册表.在这里,我写了一个代码来检查用户名,就像这样...

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();
                }
            }
        }



txtUsername字段位于AutoPostback-true中.

但是在这里,我的问题是我对用户名字段进行了另外两个验证(必填字段Validator(*输入用户名)和正则表达式验证器(*请仅使用字母(a-z),数字,点.)).在这里,必填字段验证器可以正常工作,但是当用户输入任何错误的格式(vin @ 7 ra)时,它会花费几秒钟的错误msg并显示可用的用户名.请给我一些建议.

仅当用户输入正确的格式时,才检查用户名可用性,这意味着一旦正则表达式验证正确,则仅需检查用户的可用性.

抱歉,遇到任何错误.



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.

推荐答案

Use
 Page.Validate();
 if(Page.IsValid)
{  
 write ur code here
}


也许您可以编写 ^ ]进行正则表达式检查可用性检查吗?
Maybe you could write a Custom validator[^] that does the regexp checking and the availability checking?


这篇关于关于验证和检查用户名可用性.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆