1
protected void UserId_TextBox_TextChanged(object sender, EventArgs e)
{
    SqlDataAdapter da = new SqlDataAdapter("select user_id from Userinfo where user_id='" + UserId_TextBox.Text + "@femail.com'", con);
    DataTable dt = new DataTable();
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        UserId_Label.Text = "Someone already has that username, try another?";
    }
    else 
    {
        UserId_Label.Text = "Wow , its a unique username! please fill in the remaining fields of the form";
    }

这是我用来检查用户是否已经存在的一段代码,我想在控件转到循环的 else 部分之前检查我对文本框施加的正则表达式验证器是否已验证与否...有人可以在这里帮忙吗?(我可以在 else 部分之前使用 else if 语句并检查正则表达式是否已通过某种方法验证?)

提前致谢。

4

3 回答 3

2

您可以使用 ifelse 在 else 上验证文本框正则表达式验证

 if(dt.Rows.Count > 0)
{
    UserId_Label.Text = "Someone already has that username, try another?";
}
elseif(!Page.IsValid)
{
    // Do what needs to be done when not valid
    UserId_Label.Text = "Invalid username input";
}
else
{
    UserId_Label.Text = "Wow , its a unique username! please fill in the remaining fields of the form";
}
于 2013-10-17T13:41:51.330 回答
1

用于Page.IsValid确定是否所有验证器都通过了,如下所示:

else 
{
    if(Page.IsValid)
    {
        UserId_Label.Text = "Wow , its a unique username! please fill in the remaining fields of the form";
    }
    else
    {
        // Do something here, because validation failed
    }
}
于 2013-10-17T13:37:05.977 回答
0

Page.IsValid 有一种方法:

if (Page.IsValid && Regex.IsMatch(stringtomatch,@"RegularExpression")
{
}
于 2013-10-17T13:41:07.743 回答