RegularExpressionValidator,仅适用于一家公司的多封电子邮件

时间:2012-11-27 16:09:41

标签: c# asp.net regex telerik

我使用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;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分: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;
    }
相关问题