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