3

I have a start date and end date field in a form. I have specified format for calender extender. since then, the compare validator is not working. It is always displaying the error message. Please help. I need to show the date in the format "Fri 04 May 2012".

Start date field:

<asp:TextBox ID="txtStartDate" ReadOnly="true" runat="server" 
    CssClass="textBoxWidth TPRValue" Text='<%#DataBinder.Eval(Container.DataItem, "StartDate", "{0: ddd MM dd, yyyy}")%>'>
</asp:TextBox>
<asp:ImageButton ID="imgBtnStartDate" runat="server" ImageUrl="~/Common/Images/Calendar.GIF" CausesValidation="false" ImageAlign="AbsMiddle" />
<ajax:CalendarExtender ID="StartDateCalendar" TargetControlID="txtStartDate" PopupButtonID="imgBtnStartDate" runat="server" 
    Format="ddd MM dd, yyyy">
</ajax:CalendarExtender>
<asp:CompareValidator ID="startDateCompareValidator" runat="server" ControlToValidate="txtStartDate" ControlToCompare="txtEndDate" Enabled="true" 
    Type="Date" Display="Dynamic" Operator="LessThanEqual"
    Text="Startdate should be <= enddate">
</asp:CompareValidator>

EndDate field:

<asp:TextBox ID="txtEndDate" ReadOnly="true" runat="server" 
    CssClass="textBoxWidth TPRValue" Text='<%#DataBinder.Eval(Container.DataItem, "EndDate", "{0: ddd MM dd, yyyy}")%>'>
</asp:TextBox>
<asp:ImageButton ID="imgBtnEndDate" runat="server" ImageUrl="~/Common/Images/Calendar.GIF" CausesValidation="false" ImageAlign="AbsMiddle" />
<ajax:CalendarExtender ID="EndDateCalendar" TargetControlID="txtEndDate" PopupButtonID="imgBtnEndDate" runat="server" 
    Format="ddd MM dd, yyyy">
</ajax:CalendarExtender>

Compare validator:

<asp:CompareValidator ID="startDateCompareValidator" runat="server" ControlToValidate="txtStartDate" 
    ControlToCompare="txtEndDate" Enabled="true" Type="Date" Display="Dynamic" Operator="LessThanEqual"
    Text="Startdate should be <= enddate">
</asp:CompareValidator>
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
user1107973
  • 43
  • 2
  • 12

3 Answers3

4

I assume that the CompareValidator does not accept your format.

The CompareValidator is pretty particular about the dates that it will accept. For example, the following dates are not considered valid:

  • January 1, 2001
  • Jan 1, 2001
  • Fri 04 May 2012

The CompareValidator requires a date that looks like this:

  • 1/1/2001
  • 1-1-2001
  • 5/4/2012

http://www.informit.com/articles/article.aspx?p=25461&seqNum=5

Without having tested it, you could try to use a hidden TextBox(display:none) with the accepted date format as Text. Then set the Validator's ControlToValidate to the "hiddenfield". You need to synchronize both TextBoxes' Text properties with their hiddenfields. Maybe this gives you an idea.

Edit: Ok, i've tried to get it working what i've said and actually it is working :) Maybe there's some refactoring possible, but have a look yourself.

To hide the TextBox with the working date format i've used CSS:

<style type="text/css">
    .hidden
    {
        display:none;   
    }
</style>

These JS-functions are called when the user changes a date via CalendarExtenders:

<script type="text/javascript">
    function dateChangedStart(sender, args) {
        var selectedDate = sender.get_selectedDate();
        var hiddenStart = $get("txtStartDateHidden");
        var validator = $get("startDateCompareValidator");
        hiddenStart.value = dateToString(selectedDate);
        ValidatorValidate(validator);
    }
    function dateChangedEnd(sender, args) {
        var selectedDate = sender.get_selectedDate();
        var hiddenEnd = $get("txtEndDateHidden");
        var validator = $get("startDateCompareValidator");
        hiddenEnd.value = dateToString(selectedDate);
        ValidatorValidate(validator);
    }
    function dateToString(d) {
        var year = d.getFullYear();
        var month = d.getMonth() + 1; //months are zero based
        var day = d.getDate();
        return year + "/" + month + "/" + day;
    }
</script>

This is the rest of the sample page:

<div>
    <asp:TextBox ID="txtStartDate" CausesValidation="false" ReadOnly="true" runat="server">
    </asp:TextBox>
    <asp:TextBox ID="txtStartDateHidden" CssClass="hidden" ValidationGroup="DateCheck" CausesValidation="true" ReadOnly="false" runat="server">
    </asp:TextBox>
    <ajax:CalendarExtender ID="StartDateCalendar" TargetControlID="txtStartDate" runat="server"
        OnClientDateSelectionChanged="dateChangedStart"
        Format="ddd MM dd, yyyy">
    </ajax:CalendarExtender>
    <asp:CompareValidator ID="startDateCompareValidator" runat="server" EnableClientScript="true"
        ControlToValidate="txtStartDateHidden" Display="Static" Operator="LessThanEqual" ValidationGroup="DateCheck"
        ControlToCompare="txtEndDateHidden" Enabled="true" Type="Date" Text="Startdate should be <= enddate">
    </asp:CompareValidator>
    <asp:TextBox ID="TxtEndDate" CausesValidation="false" ReadOnly="true" runat="server">
    </asp:TextBox>
    <asp:TextBox ID="txtEndDateHidden" CssClass="hidden" ValidationGroup="DateCheck" CausesValidation="true" ReadOnly="false" runat="server">
    </asp:TextBox>
    <ajax:CalendarExtender ID="EndDateCalendar" TargetControlID="txtEndDate" runat="server"
        OnClientDateSelectionChanged="dateChangedEnd"
        Format="ddd MM dd, yyyy">
    </ajax:CalendarExtender>
    <asp:Button ID="BtnSubmit" CausesValidation="true" ValidationGroup="DateCheck" runat="server" Text="Submit" />
</div>
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • how to synchronize with the hidden field and the text box? once the user selects the date from calender control, it should be assigned to both text box and the hidden field..have to try javascript – user1107973 Mar 16 '12 at 12:34
  • @user1107973: I've tried to demonstrate and it apparently works, have a look at my edited answer. – Tim Schmelter Mar 16 '12 at 14:34
  • @TimSchmelter I am trying to get the above code to work on my web page, but I don't think you're firing the javascript code at all. Shouldn't the OnClientDateSelectionChanged="dateChangedStart" be OnClientDateSelectionChanged="dateChangedStart()" but with some parameters as expected by javascript? – Fandango68 Sep 01 '14 at 06:44
  • And also shouldn't the validator be kicked in at the END date, as the end date may not always be provided before the START date. I just don't see how you claim the above works. – Fandango68 Sep 01 '14 at 06:46
  • @Fernando68: i don't understand your objection here. It's a long time ago and i cannot test it now, but i'm using the `CalendarExtender`s `OnClientDateSelectionChanged` event. The `StartDateCalendar`-calendar triggers `dateChangedStart` and `EndDateCalendar` triggers `dateChangedEnd`. I've provided a full working sample page. – Tim Schmelter Sep 01 '14 at 07:25
  • @TimSchmelter Hi Tim. My point is [1] your solution does not work and [2] logically it should be applied on the END date, not the start date and [3] while I am at it, what if the user wants to type in a date directly into the text box? – Fandango68 Sep 02 '14 at 02:53
  • @Fernando: i still cannot test it, but as far as i can see you have to validate in both cases, so if the user changes the date of the first and second calendar, since the startdate can also be later than the end date after the user changed it. If the user is allowed to change it also in the textbox you just have to trigger above code in the appropriate event, but you can also prevent that. It wasnt part of this question anyway. – Tim Schmelter Sep 02 '14 at 05:24
  • @TimSchmelter. Could you please provide us with a full sample of what you claim to work as per your line above, "Ok, i've tried to get it working what i've said and actually it is working ". I am not a Javascript guru by any stretch of the imagination, so it would really help me if you can actually get your answer to work, especially now that you said "..I still cannot test it". – Fandango68 Sep 02 '14 at 06:15
  • @Fernando68: i guess you haven't even tried the code above. I've provided a working sample page avove, added the ASPX, CSS and JS functions. I only just noticed that i've even made the `TextBoxes` readonly by setting `ReadOnly="true"` to prevent that the user can change it manually, so obviously you haven't even noticed that. **EDIT** So now i've tested it again and it works. You'll notice that you cannot select a start-date which is earlier than the end-date by using a custom date-time format that is normally not supported by the `CompareValidator`(which is the core topic of this question). – Tim Schmelter Sep 02 '14 at 07:36
0

In addition to Tim's answer, for the ones who want to use jQuery:

function dateChangedStart(sender, args) {
    var selectedDate = sender.get_selectedDate();
    var hiddenStart = $('input[id$=txtStartDateHidden]');
    var validator = $('span[id$=startDateCompareValidator]');
    hiddenStart.val(dateToString(selectedDate));

    var validatorAsDOM = validator.get(0);
    ValidatorValidate(validatorAsDOM);
}
Brabbeldas
  • 1,939
  • 4
  • 20
  • 32
  • Yeah but did you try and use Tim's answer in a sample project or something? His code won't even call the JS code, so what diff is it with jquery? – Fandango68 Sep 02 '14 at 06:16
  • @Fernando68: You haven't tried it. The js functions are triggered because they are event handlers, for example: `OnClientDateSelectionChanged="dateChangedStart"`. Do you use Ajax-`CalendarExtender`s at all? – Tim Schmelter Sep 03 '14 at 20:27
0

For date format MM/dd/yyyy

put this in Web.Config

<globalization culture="en-us"/> 

under

<system.web>
Prateek Gupta
  • 880
  • 1
  • 10
  • 21