سؤال

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

هل كانت مفيدة؟

المحلول

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>

نصائح أخرى

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>

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

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>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top