1

我正在使用 RegularExpressionValidator 来验证特定公司的电子邮件 ID。这个:

ValidationExpression=\\w+([-+.']\\w+)*@ABCCompany.com 

仅适用于第一个电子邮件 ID,我需要验证多个电子邮件。我在网上找到了另一个:

ValidationExpression="((\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*([;])*)*"

它工作正常,但我必须验证ABCCompany.com. 我也尝试CustomValidator了服务器端验证,它的工作代码很明智,但它没有显示错误消息。

谁能帮我解决我的问题。

我的 HTML 代码:

<

telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" CellPadding="0"  CellSpacing="0" GridLines="None" HorizontalAlign="Left" ShowFooter="true" AllowPaging="true"  PageSize="10" AllowMultiRowSelection="true" OnItemCommand="RadGrid1_OnItemCommand" OnNeedDataSource="RadGrid1_OnNeedDataSource" OnInsertCommand="RadGrid1_OnInsertCommand" OnUpdateCommand="RadGrid1_OnUpdateCommand" OnDeleteCommand="RadGrid1_OnDeleteCommand"  AllowSorting="true"> 

 <PagerStyle Position="Bottom" AlwaysVisible="true" /> 

 <ValidationSettings EnableValidation="true" ValidationGroup="products" /> 

<MasterTableView CommandItemDisplay="Top" HorizontalAlign="NotSet" AutoGenerateColumns="False" DataKeyNames="id"> 

 <Columns> 
<telerik:GridTemplateColumn DataField="Product_ID" FilterControlAltText="Filter Product_ID column" HeaderText="Product_ID" UniqueName="Product_ID" SortExpression="Product_ID"> 

<HeaderStyle HorizontalAlign="Center" /> 

<ItemStyle HorizontalAlign="Center" /> 

<ItemTemplate> 

<asp:Label ID="lblProduct_ID" runat="server" Text='<%# Bind("Product_ID") %>'></asp:Label> 

</ItemTemplate> 

</telerik:GridTemplateColumn> 

<telerik:GridTemplateColumn DataField="Product_Email" FilterControlAltText="Filter Product_Email column" 

HeaderText="Product Email" UniqueName="Product_Email" SortExpression="Product_Email"> 

<HeaderStyle HorizontalAlign="Left" /> 

<ItemStyle HorizontalAlign="Left" /> 

<ItemTemplate> 

<asp:Label ID="lblProduct_Email" runat="server" Text='<%# Bind("Product_Email") %>'></asp:Label> 

</ItemTemplate> 

<EditItemTemplate> 

<asp:TextBox ID="tbProduct_Email" runat="server" Text='<%# Bind("Product_Email") %>' 

Width="500px"></asp:TextBox>&nbsp;(Note: Enter multiple emails with ; separator) 

<asp:RequiredFieldValidator ID="rfvtbProduct_Email" runat="server" ControlToValidate="tbProduct_Email" 

ErrorMessage="Please enter Product Email" ForeColor="Red" ValidationGroup="products"></asp:RequiredFieldValidator>  

<asp:CustomValidator ID="cvtbProduct_Email" runat="server" ControlToValidate="tbProduct_Email" 

ForeColor="Red" ErrorMessage="Enter only valid ABCCompany Emails" OnServerValidate="cvtbProduct_Email_OnServerValidate" 

ValidationGroup="products" EnableClientScript="false" Display="None"></asp:CustomValidator> 

</EditItemTemplate> 

</telerik:GridTemplateColumn> 

<telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn"> 

<ItemStyle HorizontalAlign="Center"></ItemStyle> 

</telerik:GridEditCommandColumn> 

<telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete" Text="Delete" 

UniqueName="DeleteColumn"> 

<ItemStyle HorizontalAlign="Center"></ItemStyle> 

</telerik:GridButtonColumn> 

</Columns> 

<EditFormSettings ColumnNumber="1" CaptionDataField="Product_id" CaptionFormatString="Edit properties of Product ID: {0}"  InsertCaption="Product ID:"> 

</EditFormSettings> 

</MasterTableView> 

<ClientSettings EnableRowHoverStyle="true"> 

</ClientSettings> 

</telerik:RadGrid>

服务器端验证代码:

    protected void cvtbProduct_Email_OnServerValidate(object sender, ServerValidateEventArgs args)
    {
        bool valid;

        var email = new string[100];

        var emailList = (TextBox) RadGrid1.MasterTableView.GetInsertItem().FindControl("Product_Email");

        if (emailList.Text != "")
        {
            if (emailList.Text.Contains(";"))

                email = emailList.Text.Split(';');

            else

                email[0] = emailList.Text;

            for (var i = 0; i < email.Length; i++)
            {
                if (email[i] != null)
                {
                    valid = Regex.IsMatch(email[i], "\\w+([-+.']\\w+)*@ABCCompany.com");

                    if (!valid)
                    {
                        args.IsValid = false;
                    }

                    else

                        args.IsValid = true;
                }
            }
        }
    }
4

1 回答 1

0
    protected void cvtbProduct_Email_OnServerValidate(object sender, ServerValidateEventArgs args)
    {
        var emailList = (TextBox) RadGrid1.MasterTableView.GetInsertItem().FindControl("Product_Email");

        if (string.IsNullOrEmpty(emailList.Text))
        {
            args.IsValid = false;
            return;
        }

        //you don't need to check if it has the seperator in it
        var emails = emailList.Text.Split(';');

        foreach (var email in emails)
        {
            //the @ before a string removes the need to double up on '\'
            //you were missing a '\' before the .
            var valid = Regex.IsMatch(email, @"\w+([-+.']\w+)*@ABCCompany\.com");

            if (!valid)
            {
                args.IsValid = false;
                return; //don't check anymore
            }
        }

        //all must have passed
        args.IsValid = true;
    }
于 2012-11-27T16:27:11.780 回答