嗨,我有以下格式的日期格式,dd-MMM-yy,我使用比较验证器来验证日期,如下所示
<asp:CompareValidator ID="cmpDates" runat="server" ControlToValidate="StartDate"
SetFocusOnError="true" ControlToCompare="EndDate"
ErrorMessage="EndDate must be greater than StartDate"
Display="None" Operator="DataTypeCheck"
ValidationGroup="vg" Type="Date"
CultureInvariantValues="true">
</asp:CompareValidator>但这不是按要求工作的,所以有人可以帮助我如何以所需的格式验证日期?
发布于 2013-08-11 07:20:02
尝试一下,这里我们使用Ajax calander控件获得dd/mm/yyyy格式的输入,然后使用比较验证器
<asp:TextBox ID="txtStart" runat="server"></asp:TextBox>
<cc1:CalendarExtender ID="txtStart_CalendarExtender" runat="server"
Enabled="True" TargetControlID="txtStart">
</cc1:CalendarExtender>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="txtEnd" ControlToValidate="txtStart"
ErrorMessage="CompareValidator"></asp:CompareValidator>
</div>
<p>
<asp:TextBox ID="txtEnd" runat="server"></asp:TextBox>
<cc1:CalendarExtender ID="txtEnd_CalendarExtender" runat="server"
Enabled="True" TargetControlID="txtEnd">
</cc1:CalendarExtender>
</p>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>发布于 2013-08-11 10:28:53
像这样删除代码
<asp:CompareValidator ID="cmpDates" runat="server" ControlToValidate="StartDate"
SetFocusOnError="true" ControlToCompare="EndDate"
ErrorMessage="EndDate must be greater than StartDate"
Operator="LessThan"
ValidationGroup="vg" Type="Date"
CultureInvariantValues="true"></asp:CompareValidator>发布于 2016-01-12 05:53:08
默认情况下,CompareValidator不适用于dd/mm/yyyy格式,因此需要在ASP.Net Web Page的页指令中显式地将页的Culture属性更改为en-GB,或者可以将其添加到webconfig中。
在页面级别
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
Culture = "en-GB" %>在Webconfig
<globalization requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-GB"
uiCulture="en-GB" />https://stackoverflow.com/questions/18169609
复制相似问题