Asp.net compare validator to validate date

As you all know Compare validators can be used to validate dates and check based on operator type (<, <= , >= etc). I have set the cultureinvariantvalues="true" property to validate two textbox controls that hold dates. I have to constrain them such that the start date must be earlier than the finish date. The validation seems to fail when I type a descriptive date like below:

StartDate: Tuesday, 21 February 2012

FinishDate: Wednesday, 22 February 2012

Even though 22nd is larger than 21st the validation fails. The markup I used is below. If for any reason you need format info, here it is dddd, dd MMMM yyyy

<asp:CompareValidator id="cvtxtStartDate" runat="server" 
       controltocompare="txtFinishDate" 
       cultureinvariantvalues="true" 
       display="Dynamic" 
       enableclientscript="true" 
       controltovalidate="txtStartDate" 
       errormessage="Start date must be earlier than finish date" 
       type="Date" 
       setfocusonerror="true" 
       operator="LessThanEqual" 
       text="Start date must be earlier than finish date">

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try this approach, First Enter the Start Date and Check the Compare Validator with the End Date textbox:

<asp:CompareValidator id="cvtxtStartDate" runat="server" 
     ControlToCompare="txtStartDate" cultureinvariantvalues="true" 
     display="Dynamic" enableclientscript="true"  
     ControlToValidate="txtFinishDate" 
     ErrorMessage="Start date must be earlier than finish date"
     type="Date" setfocusonerror="true" Operator="GreaterThanEqual" 
     text="Start date must be earlier than finish date"></asp:CompareValidator>

Method 2

Compare validator has the type=date.But that date type is constrained to accept only particular format of date i.e ToShortDateString().
If the date format of the two textboxes to be compared is in some other format like ToLongDateString() or some format specified by ToString(“dd MMMM,yyyy”) the comparision does not work.
CustomValidator isonly option.
If you want to use compare validator only then

textstartdate.text=Calendar1.SelectedDate.ToShortDateString();
textfinishdate=Calendar2.SelectedDate.ToShortDateString();
<asp:CompareValidator ID="CompareValidator4" runat="server" 
                    ControlToCompare="textstartdate" ControlToValidate="textfinishdate" 
                    CultureInvariantValues="True" 
                    ErrorMessage="Date should be greater than booking date." 
                    Operator="GreaterThanEqual" SetFocusOnError="True" Type="Date"></asp:CompareValidator>

Method 3

Try custom Validator and at the code behind at onservervalidate event convert the text to DateTime and then do the comparision.

protected void DateTimeComparision_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = Convert.ToDateTime(txtStartDate.Text) < Convert.ToDateTime(txtFinishDate.Text);
    }

Method 4

function FromAndToDateValidate() {
try {
    var StartDate = new Date();
    StartDate = $("#dtpFromDate").val();

    var EndDate = new Date();
    EndDate = $("#dtpToDate").val();
    args.IsValid = (StartDate <= EndDate);
}
catch (ex) {
    alert(ex);
}
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x