CompareValidator来验证两个日期

本文关键字:两个 日期 验证 CompareValidator | 更新日期: 2023-09-27 18:27:07

嗨,我的日期格式为以下格式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>

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

CompareValidator来验证两个日期

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

像这个一样重新修改代码

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

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

我现在已经尝试过这种方法,它适用于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>