检查C#后端中正则表达式验证器的验证

时间:2013-10-17 13:28:36

标签: c# asp.net sql-server database ado.net

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语句并检查正则表达式是否已通过某种方法验证?)

提前致谢。

3 个答案:

答案 0 :(得分:2)

您可以使用if else

在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";
}

答案 1 :(得分: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
    }
}

答案 2 :(得分:0)

Page.IsValid有一种方式:

if (Page.IsValid && Regex.IsMatch(stringtomatch,@"RegularExpression")
{
}