【问题标题】:CompareValidator to validate two datesCompareValidator 验证两个日期
【发布时间】:2013-08-12 17:43:34
【问题描述】:

大家好,我的日期格式如下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>

但这不能按要求工作,所以有人可以帮助我如何验证所需格式的日期

【问题讨论】:

  • 您可以使用 CalendarExtender 并设置它的 Format 属性
  • 您好,我在这里使用 Devexpress 控件

标签: c# asp.net devexpress comparevalidator


【解决方案1】:

我已经尝试过这种方法,现在它适用于 dd-mm-yyyy 格式

在 web.config 文件中

<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB" /></system.web>

在 .aspx 页面中更新此内容时

添加 Culture = "en-GB"

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="filename.aspx.cs" Inherits="<%--your backend code--%>"   Culture = "en-GB"  %>

现在将 CompareValidator 添加到比较日期

 <asp:CompareValidator ID="CompareValidator1" ValidationGroup = "Date" ForeColor = "Red" runat="server" ControlToValidate = "startdate" ControlToCompare = "enddate" Operator = "LessThan" Type = "Date" ErrorMessage="Start date must be less than End date."></asp:CompareValidator>

【讨论】:

    【解决方案2】:

    CompareValidator默认不适用于dd/mm/yyyy格式,所以需要在ASP.Net Web Page的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" />
    

    【讨论】:

      【解决方案3】:

      这样修改代码

      <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>
      

      【讨论】:

        【解决方案4】:

        试试这个,这里我们使用 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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-06-27
          • 1970-01-01
          • 2015-04-19
          • 1970-01-01
          • 2019-09-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多