即使我使用正则表达式验证器,用户也会在电子邮件格式错误时创建

时间:2013-02-08 08:10:15

标签: c# asp.net membership

我正在尝试使用create user向导创建用户。对于电子邮件验证,我使用regualr表达式控制。我将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>

2 个答案:

答案 0 :(得分:2)

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

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

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

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

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