1

我正在尝试使用创建用户向导创建用户。对于电子邮件验证,我使用的是正则表达式控制。我ControlToValidate为电子邮件文本框的 id 赋予了属性。当我给出错误的电子邮件 ID 并单击创建用户时,它会显示我在其中配置的错误消息,<asp:RegularExpressionValidator>但它会进入下一步并说用户创建成功。 电子邮件格式错误时如何停止注册用户。我正在尝试CreatingUser像这样修改事件

protected void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e)
        {
            bool allfieldsstatus = false;
            RegularExpressionValidator emailvalidator = (RegularExpressionValidator)CreateUserWizardStep1.ContentTemplateContainer.FindControl("emailvalidator");

            if (!emailvalidator.Visible)
            {
                allfieldsstatus = true;
            }

            if (allfieldsstatus)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
        }

但这不起作用。可见性属性根本没有显示。我以另一种方式尝试了这种if情况,因为 if(!emailvalidator.ErrorMessage.length!=0) { allfieldstatus = true; } 这也不起作用。因为该ErrorMessage属性始终存在于配置中,<asp:RegularExpressionValidator > 对此有什么解决方案?

<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" 
            CssClass="createUseWizard" AutoGeneratePassword="True" ContinueDestinationPageUrl="~/Account/AdminRegister.aspx"
            CreateUserButtonText="Register User" 
            OnCreatedUser="CreateUserWizard1_CreatedUser" BorderStyle="None" 
            DisplayCancelButton="True" oncreatinguser="CreateUserWizard1_CreatingUser" 
            LoginCreatedUser="False">
            <WizardSteps>
                <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
                    <ContentTemplate>
                        <div id="registerUserDiv">
                            <div id="registerUserHeader">
                                Register New User
                            </div>
                            <div>
                                <div class="registerUserLable">
                                    <asp:Label ID="UserNameLabel" runat="server" Text="User Name" AssociatedControlID="UserName"></asp:Label>
                                </div>
                                <div class="inputTextbox">
                                    <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                    <asp:RequiredFieldValidator CssClass="showInRed" ID="UserIDrequired" runat="server" ControlToValidate="UserName"
                                        ErrorMessage="Email is required." ToolTip="User Name is required." ValidationGroup="CreateUserWizard1"
                                        SetFocusOnError="True">*</asp:RequiredFieldValidator>
                                </div>
                            </div>
                            <div>
                                <div class="registerUserLable">
                                    <asp:Label ID="Label1" runat="server" Text="E-mail" AssociatedControlID="Email"></asp:Label></div>
                            </div>
                            <div class="inputTextbox">
                                <asp:TextBox ID="Email" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator CssClass="showInRed" ID="RequiredFieldValidator1" runat="server"
                                    ControlToValidate="Email" ErrorMessage="E-mail is required." ToolTip="E-mail is required."
                                    ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>                                
                            </div>
                            <div>
                                <div class="registerUserLable">
                                    <asp:Label ID="Label2" Text="Select Role" runat="server" />
                                </div>
                                <div class="inputTextbox">
                                    <asp:DropDownList ID="rolesDropdown" runat="server">
                                    </asp:DropDownList>
                                </div>

                            </div>
                            <div>
                                <div class="errorEmail">
                                    <asp:RegularExpressionValidator ID="emailvalidator" runat="server" 
                                    ErrorMessage="Email Should be in correct format" ControlToValidate="Email" 
                                    SetFocusOnError="True" 
                                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
                                        Display="Dynamic"></asp:RegularExpressionValidator>
                                </div>
                            </div>
                            <div>
                                <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                            </div>
                        </div>
                    </ContentTemplate>
                </asp:CreateUserWizardStep>
                <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
                </asp:CompleteWizardStep>
            </WizardSteps>
        </asp:CreateUserWizard>
4

2 回答 2

2

通常,您应该检查验证是否通过IsValid单个验证器控件或页面本身的属性:

if (!emailvalidator.IsValid) {
    e.Cancel = true;
}

或者

if (!Page.IsValid) {
    e.Cancel = true;
}

对于向导,​​您可以在NextButtonClickFinishButtonClick事件处理程序中执行此检查。

于 2013-02-08T08:19:04.150 回答
0

如果您认为这是 RegExp 问题,请尝试以下操作:

/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i
于 2013-02-08T08:24:35.187 回答