CompareValidator to validate two dates

会有一股神秘感。 提交于 2020-01-06 04:06:10

问题


Hi all I am having my date format in the following format dd-MMM-yy I am using compare validator to validate the dates as follows

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

But this is not working as per required so can some one help me how Can I validate the dates in the required format


回答1:


Try this, here we use the Ajax calander control to get the input in dd/mm/yyyy format and then use the compare validator

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



回答2:


Remodify the code like this

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



回答3:


CompareValidator does not work for dd/mm/yyyy format by default,so you need to explicitly change the Culture property of the page to en-GB in the page directive of the ASP.Net Web Page or you can add it in webconfig

At Page Level:

<%@ Page Language="C#" 
    AutoEventWireup="true" 
    CodeFile="Default.aspx.cs" 
    Inherits="_Default" 
    Culture = "en-GB" %>

At Webconfig:

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



回答4:


i have tried this method now this works fine for dd-mm-yyyy format

in web.config file

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

upon updating this in .aspx page

add Culture = "en-GB"

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

now add CompareValidator to the comparing dates

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


来源:https://stackoverflow.com/questions/18169609/comparevalidator-to-validate-two-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!