将两个验证控件合并为一个

时间:2012-02-13 20:48:31

标签: asp.net validation active-directory

我有两个验证控件,我可以将它们组合成一个吗? 以下是我原来的两个控件,但它只适用于第一个。我意识到它可能只有一个验证控件。

<asp:TextBox runat="server" ID="UserName" />
                            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ValidationExpression="^[a-zA-Z0-9]{6,}$"
                                runat="server" ErrorMessage="Error, please reselect it" ControlToValidate="UserName">
                                </asp:RegularExpressionValidator>
                                <asp:CustomValidator
                                    ID="CustomValidator1" runat="server" 
                                ErrorMessage="Please select another name." ControlToValidate="UserName" OnServerValidate="ValidateUser"></asp:CustomValidator>

代码背后的验证:

 protected void ValidateUser(object source, ServerValidateEventArgs args)
    {
        // check if the username created exists in AD already. 
        TextBox UserNameTextBox =
        (TextBox)CreateUserWizardStep2.ContentTemplateContainer.FindControl("UserName");
        string UserNameCreated = UserNameTextBox.Text;
        DirectoryEntry entry = new DirectoryEntry("LDAP://cfs");
        entry.AuthenticationType = AuthenticationTypes.Secure;

        DirectorySearcher deSearch = new DirectorySearcher(entry);
        deSearch.Filter = "(&(objectClass=user)(cn=" + UserNameCreated + "))";

        SearchResultCollection results = deSearch.FindAll();
        Match match = Regex.Match(args.Value, @"^[a-zA-Z0-9]{6,}$",
    RegexOptions.IgnoreCase);

        if (results.Count > 0)
            args.IsValid = false;
        else if (match.Success)
            args.IsValid = true;
        else
            args.IsValid = false;
    }

感谢。 编辑:应用程序无法到达此处,即使我设置了断点。我删除了第一个验证控件,只保留第二个。

1 个答案:

答案 0 :(得分:1)

试试这个:让自定义验证器完成上述所有工作以及正则表达式验证器。如果您想将所有工作作为一个项目进行,这只是一种方式:

ASPX代码:

<asp:TextBox runat="server" ID="UserName" />                            
<asp:CustomValidator ID="CustomValidator1" runat="server" 
ControlToValidate="UserName" OnServerValidate="ValidateUser"></asp:CustomValidator>

ASPX.CS代码:(代码隐藏)

protected void ValidateUser(object source, ServerValidateEventArgs args)
        {
            Regex regx = new Regex("^[a-zA-Z0-9]{6,}$");
            if (regx.IsMatch(UserName.Text) == false)
            {
                CustomValidator1.ErrorMessage = "Error, please reselect it";
                args.IsValid = false;
            }
            else
            {
                // check if the username created exists in AD already.
                TextBox UserNameTextBox =
                (TextBox)CreateUserWizardStep2.ContentTemplateContainer.FindControl("UserName");
                string UserNameCreated = UserNameTextBox.Text;
                DirectoryEntry entry = new DirectoryEntry("LDAP://cfs");
                entry.AuthenticationType = AuthenticationTypes.Secure;

                DirectorySearcher deSearch = new DirectorySearcher(entry);
                deSearch.Filter = "(&(objectClass=user)(cn=" + UserNameCreated + "))";

                SearchResultCollection results = deSearch.FindAll();
                if (results.Count > 0)
                {
                    CustomValidator1.ErrorMessage = "Please select another name.";
                    args.IsValid = false;
                }
                else
                {
                    args.IsValid = true;
                }
            }
        }

这是我能想到的一些事情。让我知道你如何去做以及你的想法。