尽管我使用了regalr表达式验证器,但当电子邮件格式错误时,用户正在创建

本文关键字:错误 格式 电子邮件 用户 创建 regalr 表达式 验证 管我 | 更新日期: 2023-09-27 17:58:12

我正在尝试使用创建用户向导创建一个用户。对于电子邮件验证,我使用正则表达式控件。我为电子邮件文本框的id指定了ControlToValidate属性。当我给出错误的电子邮件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>

尽管我使用了regalr表达式验证器,但当电子邮件格式错误时,用户正在创建

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

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

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

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

如果你认为这是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