Asp.net compare validator to validate date

Deeptechtons picture Deeptechtons · Feb 21, 2012 · Viewed 53.7k times · Source

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

Answer

Ebad Masood picture Ebad Masood · Feb 21, 2012

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>